forked from julianschill/klipper-led_effect
-
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.
feature(led_effect_plugin): remove colormath dependency and replace w…
…ith copilot rgb_to_hsl code
- Loading branch information
Showing
10 changed files
with
4,266 additions
and
109 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,52 @@ | ||
def rgb_to_hsl(rgb): | ||
r, g, b = rgb | ||
r, g, b = r / 255.0, g / 255.0, b / 255.0 | ||
|
||
max_val = max(r, g, b) | ||
min_val = min(r, g, b) | ||
diff = max_val-min_val | ||
|
||
if max_val == min_val: | ||
h = 0 | ||
elif max_val == r: | ||
h = (60 * ((g-b)/diff) + 360) % 360 | ||
elif max_val == g: | ||
h = (60 * ((b-r)/diff) + 120) % 360 | ||
elif max_val == b: | ||
h = (60 * ((r-g)/diff) + 240) % 360 | ||
|
||
l = (max_val + min_val) / 2 | ||
|
||
if max_val == min_val: | ||
s = 0 | ||
elif l <= 0.5: | ||
s = diff / (max_val + min_val) | ||
else: | ||
s = diff / (2 - max_val - min_val) | ||
|
||
return h, s, l | ||
|
||
|
||
def hsl_to_rgb(hsl): | ||
h, s, l = hsl | ||
|
||
c = (1 - abs(2*l - 1)) * s | ||
x = c * (1 - abs((h / 60) % 2 - 1)) | ||
m = l - c/2 | ||
|
||
if h < 60: | ||
r, g, b = c, x, 0 | ||
elif h < 120: | ||
r, g, b = x, c, 0 | ||
elif h < 180: | ||
r, g, b = 0, c, x | ||
elif h < 240: | ||
r, g, b = 0, x, c | ||
elif h < 300: | ||
r, g, b = x, 0, c | ||
else: | ||
r, g, b = c, 0, x | ||
|
||
r, g, b = (r + m) * 255, (g + m) * 255, (b + m) * 255 | ||
|
||
return round(r), round(g), round(b) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,26 @@ | ||
import pytest | ||
from colormath.color_conversions import convert_color | ||
from colormath.color_objects import ( | ||
sRGBColor, | ||
LabColor, | ||
HSLColor | ||
) | ||
from hypothesis import given, settings, strategies as st | ||
from hypothesis import assume as hypothesis_assume | ||
|
||
from led_effect import color_conversions | ||
|
||
|
||
|
||
@given(st.integers(0, 255), st.integers(0, 255), st.integers(0, 255)) | ||
@settings(max_examples=1000) | ||
def test_rgb_to_hsl(r, g, b): | ||
rgb = (r, g, b) | ||
hsl = color_conversions.rgb_to_hsl(rgb) | ||
|
||
hsl_test = convert_color(sRGBColor(r/255, g/255, b/255), HSLColor) | ||
hsl_test = (hsl_test.hsl_h, hsl_test.hsl_s, hsl_test.hsl_l) | ||
assert hsl == pytest.approx(hsl_test) | ||
|
||
back_to_rgb = color_conversions.hsl_to_rgb(hsl) | ||
assert back_to_rgb == rgb |
Oops, something went wrong.