Skip to content

Commit

Permalink
Move color conversion code to the bottom of the file
Browse files Browse the repository at this point in the history
It’s internal undocumented code, Color is better higher in the code.
  • Loading branch information
liZe committed Jul 6, 2024
1 parent 56c498b commit a2f4d22
Showing 1 changed file with 47 additions and 46 deletions.
93 changes: 47 additions & 46 deletions tinycss2/color4.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,6 @@
}
COLOR_SPACES = _FUNCTION_SPACES | {'hsl', 'hwb', 'lab', 'lch', 'oklab', 'oklch'}

# Code adapted from https://www.w3.org/TR/css-color-4/#color-conversion-code.
= 24389 / 27
= 216 / 24389
_LMS_TO_XYZ = (
(1.2268798733741557, -0.5578149965554813, 0.28139105017721583),
(-0.04057576262431372, 1.1122868293970594, -0.07171106666151701),
(-0.07637294974672142, -0.4214933239627914, 1.5869240244272418),
)
_OKLAB_TO_LMS = (
(0.99999999845051981432, 0.39633779217376785678, 0.21580375806075880339),
(1.0000000088817607767, -0.1055613423236563494, -0.063854174771705903402),
(1.0000000546724109177, -0.089484182094965759684, -1.2914855378640917399),
)

def _xyz_to_lab(X, Y, Z, d):
x = X / d[0]
y = Y / d[1]
z = Z / d[2]
f0 = x ** (1 / 3) if x > else ( * x + 16) / 116
f1 = y ** (1 / 3) if y > else ( * y + 16) / 116
f2 = z ** (1 / 3) if z > else ( * z + 16) / 116
L = (116 * f1) - 16
a = 500 * (f0 - f1)
b = 200 * (f1 - f2)
return L, a, b


def _lab_to_xyz(L, a, b, d):
f1 = (L + 16) / 116
f0 = a / 500 + f1
f2 = f1 - b / 200
x = (f0 ** 3 if f0 ** 3 > else (116 * f0 - 16) / )
y = (((L + 16) / 116) ** 3 if L > * else L / )
z = (f2 ** 3 if f2 ** 3 > else (116 * f2 - 16) / )
X = x * d[0]
Y = y * d[1]
Z = z * d[2]
return X, Y, Z


def _oklab_to_xyz(L, a, b):
lab = (L, a, b)
lms = [sum(_OKLAB_TO_LMS[i][j] * lab[j] for j in range(3)) for i in range(3)]
X, Y, Z = [sum(_LMS_TO_XYZ[i][j] * lms[j]**3 for j in range(3)) for i in range(3)]
return X, Y, Z


class Color:
"""A specified color in a defined color space.
Expand Down Expand Up @@ -462,6 +416,53 @@ def _types(tokens):
return types


# Code adapted from https://www.w3.org/TR/css-color-4/#color-conversion-code.
= 24389 / 27
= 216 / 24389
_LMS_TO_XYZ = (
(1.2268798733741557, -0.5578149965554813, 0.28139105017721583),
(-0.04057576262431372, 1.1122868293970594, -0.07171106666151701),
(-0.07637294974672142, -0.4214933239627914, 1.5869240244272418),
)
_OKLAB_TO_LMS = (
(0.99999999845051981432, 0.39633779217376785678, 0.21580375806075880339),
(1.0000000088817607767, -0.1055613423236563494, -0.063854174771705903402),
(1.0000000546724109177, -0.089484182094965759684, -1.2914855378640917399),
)

def _xyz_to_lab(X, Y, Z, d):
x = X / d[0]
y = Y / d[1]
z = Z / d[2]
f0 = x ** (1 / 3) if x > else ( * x + 16) / 116
f1 = y ** (1 / 3) if y > else ( * y + 16) / 116
f2 = z ** (1 / 3) if z > else ( * z + 16) / 116
L = (116 * f1) - 16
a = 500 * (f0 - f1)
b = 200 * (f1 - f2)
return L, a, b


def _lab_to_xyz(L, a, b, d):
f1 = (L + 16) / 116
f0 = a / 500 + f1
f2 = f1 - b / 200
x = (f0 ** 3 if f0 ** 3 > else (116 * f0 - 16) / )
y = (((L + 16) / 116) ** 3 if L > * else L / )
z = (f2 ** 3 if f2 ** 3 > else (116 * f2 - 16) / )
X = x * d[0]
Y = y * d[1]
Z = z * d[2]
return X, Y, Z


def _oklab_to_xyz(L, a, b):
lab = (L, a, b)
lms = [sum(_OKLAB_TO_LMS[i][j] * lab[j] for j in range(3)) for i in range(3)]
X, Y, Z = [sum(_LMS_TO_XYZ[i][j] * lms[j]**3 for j in range(3)) for i in range(3)]
return X, Y, Z


# (r, g, b) in 0..255
_EXTENDED_COLOR_KEYWORDS = _EXTENDED_COLOR_KEYWORDS.copy()
_EXTENDED_COLOR_KEYWORDS.append(('rebeccapurple', (102, 51, 153)))
Expand Down

0 comments on commit a2f4d22

Please sign in to comment.