BGColor.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import SwiftUI
  2. // Helper function to decide how to pick the BG color
  3. public func setBGColor(bgValue: Int, lowGlucose: Int, highGlucose: Int, targetGlucose: Int) -> Color {
  4. // TODO:
  5. // Only use setDynamicBGColor if the setting is enabled in preferences
  6. if true {
  7. return setDynamicBGColor(bgValue: bgValue, lowGlucose: lowGlucose, highGlucose: highGlucose, targetGlucose: targetGlucose)
  8. }
  9. // Otheriwse, use static (orange = high, red = low, green = range)
  10. else {
  11. if bgValue > Int(highGlucose) {
  12. return Color.orange
  13. } else if bgValue < Int(lowGlucose) {
  14. return Color.red
  15. } else {
  16. return Color.green
  17. }
  18. }
  19. }
  20. // Dynamic color - Define the hue values for the key points
  21. // We'll shift color gradually one BG point at a time
  22. // We'll shift through the rainbow colors of ROY-G-BIV from low to high
  23. // Start at red for lowGlucose, green for targetGlucose, and violet for highGlucose
  24. public func setDynamicBGColor(bgValue: Int, lowGlucose: Int, highGlucose: Int, targetGlucose: Int) -> Color {
  25. let redHue: CGFloat = 0.0 / 360.0 // 0 degrees
  26. let greenHue: CGFloat = 120.0 / 360.0 // 120 degrees
  27. let purpleHue: CGFloat = 270.0 / 360.0 // 270 degrees
  28. // Calculate the hue based on the bgLevel
  29. var hue: CGFloat
  30. if bgValue <= lowGlucose {
  31. hue = redHue
  32. } else if bgValue >= highGlucose {
  33. hue = purpleHue
  34. } else if bgValue <= targetGlucose {
  35. // Interpolate between red and green
  36. let ratio = CGFloat(bgValue - lowGlucose) / CGFloat(targetGlucose - lowGlucose)
  37. hue = redHue + ratio * (greenHue - redHue)
  38. } else {
  39. // Interpolate between green and purple
  40. let ratio = CGFloat(bgValue - targetGlucose) / CGFloat(highGlucose - targetGlucose)
  41. hue = greenHue + ratio * (purpleHue - greenHue)
  42. }
  43. // Return the color with full saturation and brightness
  44. let color = Color(hue: hue, saturation: 0.6, brightness: 0.9)
  45. return color
  46. }