Skip to content

Instantly share code, notes, and snippets.

@remixer-dec
Created July 25, 2020 19:48
Show Gist options
  • Save remixer-dec/905e345ec07c3b01c3f1e45621c528c3 to your computer and use it in GitHub Desktop.
Save remixer-dec/905e345ec07c3b01c3f1e45621c528c3 to your computer and use it in GitHub Desktop.
HSL to RGB, RGB to INT convertion for micropython
#input: [H (0 to 360), S (0 to 100), L (0 to 100)]
#output: [R (0 to 255), G (0 to 255), B (0 to 255)]
#reference: https://gist.github.com/PaulKinlan/d4053acfffd49abfa197a8d370a18337
def hsl_to_rgb(HSLlist):
H, S, L = HSLlist
S = S / 100
L = L / 100
C = (1 - abs(2 * L - 1)) * S
X = C * (1 - abs(((H / 60) % 2) - 1))
m = L - C / 2;
[r1, g1, b1] = [C, X, 0] if H >= 0 and H < 60 else \
[X, C, 0] if H >= 60 and H < 120 else \
[0, C, X] if H >= 120 and H < 180 else \
[0, X, C] if H >= 180 and H < 240 else \
[X, 0, C] if H >= 240 and H < 300 else [C, 0, X]
return [int((r1 + m) * 255), int((g1 + m) * 255), int((b1 + m) * 255)]
def rgb_to_int(RGBlist):
r, g, b = RGBlist
return (r << 0x10) + (g << 0x8) + b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment