Better color temperature approximation + sine weighted intensity

This commit is contained in:
Admar Schoonen 2023-05-05 22:44:13 +02:00
parent 8c4381f72b
commit c52c355299
2 changed files with 69 additions and 13 deletions

View File

@ -11,7 +11,7 @@ from kivy.properties import ObjectProperty
import time
from sounds import WakeUpSounds
import json
from hsluv import hsluv_to_rgb
from hsluv import hsluv_to_rgb, rgb_to_hsluv
def is_arm():
if (uname()[4][:3] == 'arm') or (uname()[4][:7] == 'aarch64'):
@ -627,10 +627,66 @@ class MyClockWidget(FloatLayout):
elif intensity > 1:
intensity = 1
h = max(0, min(75, (75 - 12) * intensity + 12))
s = max(0, min(100, 250 - 250 * intensity))
l = 100 * intensity
# sunlight intensity and color temperature vary with sine function
intensity = math.sin(2 * math.pi * intensity / 4)
# from https://tannerhelland.com/2012/09/18/convert-temperature-rgb-algorithm-code.html
# usable between 1000 and 40000 K
# we only want color temperatures between 2000 and 6500 K
CCT = 2000 + (6500 - 2000) * intensity
if CCT < 1000:
CCT = 1000
if CCT > 40000:
CCT = 40000
Temperature = CCT / 100
if Temperature <= 66:
Red = 255
else:
Red = Temperature - 60
Red = 329.698727446 * pow(Red, -0.1332047592)
if Red < 0:
Red = 0
if Red > 255:
Red = 255
if Temperature <= 66:
Green = Temperature
Green = 99.4708025861 * math.log(Green) - 161.1195681661
if Green < 0:
Green = 0
if Green > 255:
Green = 255
else:
Green = Temperature - 60
Green = 288.1221695283 * pow(Green, -0.0755148492)
if Green < 0:
Green = 0
if Green > 255:
Green = 255
if Temperature >= 66:
Blue = 255
else:
if Temperature <= 19:
Blue = 0
else:
Blue = Temperature - 10
Blue = 138.5177312231 * math.log(Blue) - 305.0447927307
if Blue < 0:
Blue = 0
if Blue > 255:
Blue = 255
rgb_cct = [Red / 255, Green / 255, Blue / 255]
# adjust intensity
[h, s, l]= rgb_to_hsluv(rgb_cct)
l = l * intensity
rgb = hsluv_to_rgb([h, s, l])
return rgb

View File

@ -1,11 +1,11 @@
{
"theme": "Light",
"volume": 13,
"wake_up_sound": "NPO Radio 1",
"wake_up_brightness": 4,
"reading_light_brightness": 13,
"display_brightness": 13,
"alarm_activated": true,
"alarm_time_hour": 6,
"alarm_time_minute": 57
"theme": "Automatic",
"volume": 1,
"wake_up_sound": "Birds",
"wake_up_brightness": 20,
"reading_light_brightness": 2,
"display_brightness": 20,
"alarm_activated": false,
"alarm_time_hour": 9,
"alarm_time_minute": 9
}