BGColor.swift 2.4 KB

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