forked from nightscout/Trio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nightscout#49 from aug0211/auggie-dynamic-bg-color
Add Dynamic BG Color
- Loading branch information
Showing
25 changed files
with
555 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Foundation | ||
import SwiftUI | ||
|
||
// Helper function to decide how to pick the glucose color | ||
public func getDynamicGlucoseColor( | ||
glucoseValue: Decimal, | ||
highGlucoseColorValue: Decimal, | ||
lowGlucoseColorValue: Decimal, | ||
targetGlucose: Decimal, | ||
glucoseColorScheme: GlucoseColorScheme, | ||
offset: Decimal | ||
) -> Color { | ||
// Only use calculateHueBasedGlucoseColor if the setting is enabled in preferences | ||
if glucoseColorScheme == .dynamicColor { | ||
return calculateHueBasedGlucoseColor( | ||
glucoseValue: glucoseValue, | ||
highGlucose: highGlucoseColorValue + (offset * 1.75), | ||
lowGlucose: lowGlucoseColorValue - offset, | ||
targetGlucose: targetGlucose | ||
) | ||
} | ||
// Otheriwse, use static (orange = high, red = low, green = range) | ||
else { | ||
if glucoseValue >= highGlucoseColorValue { | ||
return Color.orange | ||
} else if glucoseValue <= lowGlucoseColorValue { | ||
return Color.red | ||
} else { | ||
return Color.green | ||
} | ||
} | ||
} | ||
|
||
// Dynamic color - Define the hue values for the key points | ||
// We'll shift color gradually one glucose point at a time | ||
// We'll shift through the rainbow colors of ROY-G-BIV from low to high | ||
// Start at red for lowGlucose, green for targetGlucose, and violet for highGlucose | ||
public func calculateHueBasedGlucoseColor( | ||
glucoseValue: Decimal, | ||
highGlucose: Decimal, | ||
lowGlucose: Decimal, | ||
targetGlucose: Decimal | ||
) -> Color { | ||
let redHue: CGFloat = 0.0 / 360.0 // 0 degrees | ||
let greenHue: CGFloat = 120.0 / 360.0 // 120 degrees | ||
let purpleHue: CGFloat = 270.0 / 360.0 // 270 degrees | ||
|
||
// Calculate the hue based on the bgLevel | ||
var hue: CGFloat | ||
if glucoseValue <= lowGlucose { | ||
hue = redHue | ||
} else if glucoseValue >= highGlucose { | ||
hue = purpleHue | ||
} else if glucoseValue <= targetGlucose { | ||
// Interpolate between red and green | ||
let ratio = CGFloat(truncating: (glucoseValue - lowGlucose) / (targetGlucose - lowGlucose) as NSNumber) | ||
|
||
hue = redHue + ratio * (greenHue - redHue) | ||
} else { | ||
// Interpolate between green and purple | ||
let ratio = CGFloat(truncating: (glucoseValue - targetGlucose) / (highGlucose - targetGlucose) as NSNumber) | ||
hue = greenHue + ratio * (purpleHue - greenHue) | ||
} | ||
// Return the color with full saturation and brightness | ||
let color = Color(hue: hue, saturation: 0.6, brightness: 0.9) | ||
return color | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// GlucoseColorScheme.swift | ||
// FreeAPS | ||
// | ||
// Created by Cengiz Deniz on 27.09.24. | ||
// | ||
import Foundation | ||
|
||
public enum GlucoseColorScheme: String, JSON, CaseIterable, Identifiable, Codable, Hashable { | ||
public var id: String { rawValue } | ||
case staticColor | ||
case dynamicColor | ||
|
||
var displayName: String { | ||
switch self { | ||
case .staticColor: | ||
return "Static" | ||
case .dynamicColor: | ||
return "Dynamic" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.