Skip to content

Commit

Permalink
feature(led_effect_plugin): remove colormath dependency and replace w…
Browse files Browse the repository at this point in the history
…ith copilot rgb_to_hsl code
  • Loading branch information
reemo3dp committed Mar 17, 2024
1 parent 96cae9e commit 2ecf060
Show file tree
Hide file tree
Showing 10 changed files with 4,266 additions and 109 deletions.
36 changes: 0 additions & 36 deletions .github/workflows/update_requirements.yaml

This file was deleted.

8 changes: 0 additions & 8 deletions install-led_effect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,6 @@ link_extension()
echo "[OK]"
}

install_requirements()
(
echo -n "Installing requirements... "
source "${VENV_PATH}/bin/activate"
pip install -r "${SRCDIR}/packages/led_effect/requirements.txt"
echo "[OK]"
)

# Restart moonraker
restart_moonraker()
Expand Down Expand Up @@ -196,7 +189,6 @@ check_folders
[ $RESTART_SERVICE -eq 1 ] && stop_klipper
if [ ! $UNINSTALL ]; then
link_extension
install_requirements
[ $UPDATE_MOONRAKER -eq 1 ] && add_updater
else
uninstall
Expand Down
52 changes: 52 additions & 0 deletions packages/led_effect/led_effect/color_conversions.py
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)
26 changes: 9 additions & 17 deletions packages/led_effect/led_effect/led_effect_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
HSLColor
)
from . import layer_parser
from . import color_conversions

ANALOG_SAMPLE_TIME = 0.001
ANALOG_SAMPLE_COUNT = 5
Expand Down Expand Up @@ -612,25 +613,16 @@ def _mixColorInColorSpace(self, color1, color2, r, colorSpace):
if colorSpace == "none":
return color1 if r <= 0.5 else color2

a = color1[3]*(1-r) + color2[3]*r
c1 = sRGBColor(color1[0], color1[1], color1[2])
c2 = sRGBColor(color2[0], color2[1], color2[2])

if colorSpace == 'hsl':
c1 = convert_color(c1, HSLColor).get_value_tuple()
c2 = convert_color(c2, HSLColor).get_value_tuple()
mix = [((1-r)*c1[m] + r*c2[m]) for m in range(3)]
mix = HSLColor(mix[0], mix[1], mix[2])
mix = convert_color(mix, sRGBColor).get_value_tuple()
return [mix[0], mix[1], mix[2], a]

if colorSpace == 'lab':
c1 = convert_color(c1, LabColor).get_value_tuple()
c2 = convert_color(c2, LabColor).get_value_tuple()
a = color1[3]*(1-r) + color2[3]*r
c1 = [c*255 for c in color1[:3]]
c2 = [c*255 for c in color2[:3]]
c1 = color_conversions.rgb_to_hsl(c1)
c2 = color_conversions.rgb_to_hsl(c2)
mix = [((1-r)*c1[m] + r*c2[m]) for m in range(3)]
mix = LabColor(mix[0], mix[1], mix[2])
mix = convert_color(mix, sRGBColor).get_value_tuple()
return [mix[0], mix[1], mix[2], a]
mix = color_conversions.hsl_to_rgb(mix)
print(mix)
return [mix[0]/255, mix[1]/255, mix[2]/255, a]


return [((1-r)*color1[m] + r*color2[m]) for m in range(4)]
Expand Down
73 changes: 68 additions & 5 deletions packages/led_effect/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/led_effect/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ description = ""

[tool.poetry.dependencies]
python = "<3.12,>=3.8"
colormath = "^3.0.0"

[tool.poetry.group.dev.dependencies]
lark = "^1.1.9"
pytest = "^8.0.1"
pytest-golden = "^0.2.2"
klippermock = {path = "../klippermock", develop = true}
hypothesis = "^6.99.6"
colormath = "^3.0.0"

[build-system]
requires = ["poetry-core"]
Expand Down
34 changes: 0 additions & 34 deletions packages/led_effect/requirements.txt

This file was deleted.

26 changes: 26 additions & 0 deletions packages/led_effect/tests/colorspace_test.py
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
Loading

0 comments on commit 2ecf060

Please sign in to comment.