Kleur.py, but its based on kleur.lua
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
import doctest | |
def kleur(h: float): | |
""" | |
This is a hue function that converts a float in the range of [0-1] to a string | |
Behold! The Doctest! | |
>>> kleur(0.598) | |
'i\\x00ÿ' | |
>>> kleur(0.5985) | |
'h\\x00ÿ' | |
>>> kleur(0.599) | |
'g\\x00ÿ' | |
>>> kleur(0.600) | |
'f\\x00ÿ' | |
>>> kleur(0.60002) | |
'e\\x00ÿ' | |
>>> kleur(0.298) | |
'ÿ6\\x00' | |
>>> kleur(0.2985) | |
'ÿ5\\x00' | |
>>> kleur(0.299) | |
'ÿ4\\x00' | |
>>> kleur(0.300) | |
'ÿ3\\x00' | |
>>> kleur(0.30002) | |
'ÿ2\\x00' | |
'\\x00' is the escaped version of '\x00', which is just hexadecimal for the NULL character (used in most languages as the End Of Line character) | |
The opposite of chr() is ord() | |
Change the output type as needed - no need to force yourself to convert the string to something else. | |
""" | |
if h < 0 > h: | |
# You can also replace this with clamping functionality (if value > 1, value = 0 and if value < 0, value = 0) | |
raise ValueError("Input is outside of range: [0-1]") | |
r, g, b = 0, 0, 0 | |
if h < 1 / 3: | |
r = 2 - h * 6 | |
g = h * 6 | |
b = 0 | |
elif h < 2 / 3: | |
r = 0 | |
g = 4 - h * 6 | |
b = h * 6 - 2 | |
else: | |
r = h * 6 - 4 | |
g = 0 | |
b = (1 - h) * 6 | |
if r > 1: | |
r = 1 | |
if g > 1: | |
g = 1 | |
if b > 1: | |
b = 1 | |
r = r * 255 | |
g = g * 255 | |
b = b * 255 | |
return f'{chr(int(g))}{chr(int(r))}{chr(int(b))}' | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment