|
| 1 | +import operator |
| 2 | +import math |
| 3 | + |
| 4 | +__version__ = "2.1.0" |
| 5 | + |
| 6 | + |
| 7 | +m = [ |
| 8 | + [3.2406, -1.5372, -0.4986], |
| 9 | + [-0.9689, 1.8758, 0.0415], |
| 10 | + [0.0557, -0.2040, 1.0570] |
| 11 | +] |
| 12 | + |
| 13 | +m_inv = [ |
| 14 | + [0.4124, 0.3576, 0.1805], |
| 15 | + [0.2126, 0.7152, 0.0722], |
| 16 | + [0.0193, 0.1192, 0.9505] |
| 17 | +] |
| 18 | + |
| 19 | +# Hard-coded D65 illuminant |
| 20 | +refX = 0.95047 |
| 21 | +refY = 1.00000 |
| 22 | +refZ = 1.08883 |
| 23 | +refU = 0.19784 |
| 24 | +refV = 0.46834 |
| 25 | +lab_e = 0.008856 |
| 26 | +lab_k = 903.3 |
| 27 | + |
| 28 | + |
| 29 | +# Public API |
| 30 | + |
| 31 | +def husl_to_rgb(h, s, l): |
| 32 | + return lch_to_rgb(*husl_to_lch([h, s, l])) |
| 33 | + |
| 34 | + |
| 35 | +def husl_to_hex(h, s, l): |
| 36 | + return rgb_to_hex(husl_to_rgb(h, s, l)) |
| 37 | + |
| 38 | + |
| 39 | +def rgb_to_husl(r, g, b): |
| 40 | + return lch_to_husl(rgb_to_lch(r, g, b)) |
| 41 | + |
| 42 | + |
| 43 | +def hex_to_husl(hex): |
| 44 | + return rgb_to_husl(*hex_to_rgb(hex)) |
| 45 | + |
| 46 | + |
| 47 | +def huslp_to_rgb(h, s, l): |
| 48 | + return lch_to_rgb(*huslp_to_lch([h, s, l])) |
| 49 | + |
| 50 | + |
| 51 | +def huslp_to_hex(h, s, l): |
| 52 | + return rgb_to_hex(huslp_to_rgb(h, s, l)) |
| 53 | + |
| 54 | + |
| 55 | +def rgb_to_huslp(r, g, b): |
| 56 | + return lch_to_huslp(rgb_to_lch(r, g, b)) |
| 57 | + |
| 58 | + |
| 59 | +def hex_to_huslp(hex): |
| 60 | + return rgb_to_huslp(*hex_to_rgb(hex)) |
| 61 | + |
| 62 | + |
| 63 | +def lch_to_rgb(l, c, h): |
| 64 | + return xyz_to_rgb(luv_to_xyz(lch_to_luv([l, c, h]))) |
| 65 | + |
| 66 | + |
| 67 | +def rgb_to_lch(r, g, b): |
| 68 | + return luv_to_lch(xyz_to_luv(rgb_to_xyz([r, g, b]))) |
| 69 | + |
| 70 | + |
| 71 | +def max_chroma(L, H): |
| 72 | + hrad = math.radians(H) |
| 73 | + sinH = (math.sin(hrad)) |
| 74 | + cosH = (math.cos(hrad)) |
| 75 | + sub1 = (math.pow(L + 16, 3.0) / 1560896.0) |
| 76 | + sub2 = sub1 if sub1 > 0.008856 else (L / 903.3) |
| 77 | + result = float("inf") |
| 78 | + for row in m: |
| 79 | + m1 = row[0] |
| 80 | + m2 = row[1] |
| 81 | + m3 = row[2] |
| 82 | + top = ((0.99915 * m1 + 1.05122 * m2 + 1.14460 * m3) * sub2) |
| 83 | + rbottom = (0.86330 * m3 - 0.17266 * m2) |
| 84 | + lbottom = (0.12949 * m3 - 0.38848 * m1) |
| 85 | + bottom = (rbottom * sinH + lbottom * cosH) * sub2 |
| 86 | + |
| 87 | + for t in (0.0, 1.0): |
| 88 | + C = (L * (top - 1.05122 * t) / (bottom + 0.17266 * sinH * t)) |
| 89 | + if C > 0.0 and C < result: |
| 90 | + result = C |
| 91 | + return result |
| 92 | + |
| 93 | + |
| 94 | +def _hrad_extremum(L): |
| 95 | + lhs = (math.pow(L, 3.0) + 48.0 * math.pow(L, 2.0) + 768.0 * L + 4096.0) / 1560896.0 |
| 96 | + rhs = 1107.0 / 125000.0 |
| 97 | + sub = lhs if lhs > rhs else 10.0 * L / 9033.0 |
| 98 | + chroma = float("inf") |
| 99 | + result = None |
| 100 | + for row in m: |
| 101 | + for limit in (0.0, 1.0): |
| 102 | + [m1, m2, m3] = row |
| 103 | + top = -3015466475.0 * m3 * sub + 603093295.0 * m2 * sub - 603093295.0 * limit |
| 104 | + bottom = 1356959916.0 * m1 * sub - 452319972.0 * m3 * sub |
| 105 | + hrad = math.atan2(top, bottom) |
| 106 | + # This is a math hack to deal with tan quadrants, I'm too lazy to figure |
| 107 | + # out how to do this properly |
| 108 | + if limit == 0.0: |
| 109 | + hrad += math.pi |
| 110 | + test = max_chroma(L, math.degrees(hrad)) |
| 111 | + if test < chroma: |
| 112 | + chroma = test |
| 113 | + result = hrad |
| 114 | + return result |
| 115 | + |
| 116 | + |
| 117 | +def max_chroma_pastel(L): |
| 118 | + H = math.degrees(_hrad_extremum(L)) |
| 119 | + return max_chroma(L, H) |
| 120 | + |
| 121 | + |
| 122 | +def dot_product(a, b): |
| 123 | + return sum(map(operator.mul, a, b)) |
| 124 | + |
| 125 | + |
| 126 | +def f(t): |
| 127 | + if t > lab_e: |
| 128 | + return (math.pow(t, 1.0 / 3.0)) |
| 129 | + else: |
| 130 | + return (7.787 * t + 16.0 / 116.0) |
| 131 | + |
| 132 | + |
| 133 | +def f_inv(t): |
| 134 | + if math.pow(t, 3.0) > lab_e: |
| 135 | + return (math.pow(t, 3.0)) |
| 136 | + else: |
| 137 | + return (116.0 * t - 16.0) / lab_k |
| 138 | + |
| 139 | + |
| 140 | +def from_linear(c): |
| 141 | + if c <= 0.0031308: |
| 142 | + return 12.92 * c |
| 143 | + else: |
| 144 | + return (1.055 * math.pow(c, 1.0 / 2.4) - 0.055) |
| 145 | + |
| 146 | + |
| 147 | +def to_linear(c): |
| 148 | + a = 0.055 |
| 149 | + |
| 150 | + if c > 0.04045: |
| 151 | + return (math.pow((c + a) / (1.0 + a), 2.4)) |
| 152 | + else: |
| 153 | + return (c / 12.92) |
| 154 | + |
| 155 | + |
| 156 | +def rgb_prepare(triple): |
| 157 | + ret = [] |
| 158 | + for ch in triple: |
| 159 | + ch = round(ch, 3) |
| 160 | + |
| 161 | + if ch < -0.0001 or ch > 1.0001: |
| 162 | + raise Exception("Illegal RGB value %f" % ch) |
| 163 | + |
| 164 | + if ch < 0: |
| 165 | + ch = 0 |
| 166 | + if ch > 1: |
| 167 | + ch = 1 |
| 168 | + |
| 169 | + # Fix for Python 3 which by default rounds 4.5 down to 4.0 |
| 170 | + # instead of Python 2 which is rounded to 5.0 which caused |
| 171 | + # a couple off by one errors in the tests. Tests now all pass |
| 172 | + # in Python 2 and Python 3 |
| 173 | + ret.append(round(ch * 255 + 0.001, 0)) |
| 174 | + |
| 175 | + return ret |
| 176 | + |
| 177 | + |
| 178 | +def hex_to_rgb(hex): |
| 179 | + if hex.startswith('#'): |
| 180 | + hex = hex[1:] |
| 181 | + r = int(hex[0:2], 16) / 255.0 |
| 182 | + g = int(hex[2:4], 16) / 255.0 |
| 183 | + b = int(hex[4:6], 16) / 255.0 |
| 184 | + return [r, g, b] |
| 185 | + |
| 186 | + |
| 187 | +def rgb_to_hex(triple): |
| 188 | + [r, g, b] = triple |
| 189 | + return '#%02x%02x%02x' % tuple(rgb_prepare([r, g, b])) |
| 190 | + |
| 191 | + |
| 192 | +def xyz_to_rgb(triple): |
| 193 | + xyz = map(lambda row: dot_product(row, triple), m) |
| 194 | + return list(map(from_linear, xyz)) |
| 195 | + |
| 196 | + |
| 197 | +def rgb_to_xyz(triple): |
| 198 | + rgbl = list(map(to_linear, triple)) |
| 199 | + return list(map(lambda row: dot_product(row, rgbl), m_inv)) |
| 200 | + |
| 201 | + |
| 202 | +def xyz_to_luv(triple): |
| 203 | + X, Y, Z = triple |
| 204 | + |
| 205 | + if X == Y == Z == 0.0: |
| 206 | + return [0.0, 0.0, 0.0] |
| 207 | + |
| 208 | + varU = (4.0 * X) / (X + (15.0 * Y) + (3.0 * Z)) |
| 209 | + varV = (9.0 * Y) / (X + (15.0 * Y) + (3.0 * Z)) |
| 210 | + L = 116.0 * f(Y / refY) - 16.0 |
| 211 | + |
| 212 | + # Black will create a divide-by-zero error |
| 213 | + if L == 0.0: |
| 214 | + return [0.0, 0.0, 0.0] |
| 215 | + |
| 216 | + U = 13.0 * L * (varU - refU) |
| 217 | + V = 13.0 * L * (varV - refV) |
| 218 | + |
| 219 | + return [L, U, V] |
| 220 | + |
| 221 | + |
| 222 | +def luv_to_xyz(triple): |
| 223 | + L, U, V = triple |
| 224 | + |
| 225 | + if L == 0: |
| 226 | + return [0.0, 0.0, 0.0] |
| 227 | + |
| 228 | + varY = f_inv((L + 16.0) / 116.0) |
| 229 | + varU = U / (13.0 * L) + refU |
| 230 | + varV = V / (13.0 * L) + refV |
| 231 | + Y = varY * refY |
| 232 | + X = 0.0 - (9.0 * Y * varU) / ((varU - 4.0) * varV - varU * varV) |
| 233 | + Z = (9.0 * Y - (15.0 * varV * Y) - (varV * X)) / (3.0 * varV) |
| 234 | + |
| 235 | + return [X, Y, Z] |
| 236 | + |
| 237 | + |
| 238 | +def luv_to_lch(triple): |
| 239 | + L, U, V = triple |
| 240 | + |
| 241 | + C = (math.pow(math.pow(U, 2) + math.pow(V, 2), (1.0 / 2.0))) |
| 242 | + hrad = (math.atan2(V, U)) |
| 243 | + H = math.degrees(hrad) |
| 244 | + if H < 0.0: |
| 245 | + H = 360.0 + H |
| 246 | + |
| 247 | + return [L, C, H] |
| 248 | + |
| 249 | + |
| 250 | +def lch_to_luv(triple): |
| 251 | + L, C, H = triple |
| 252 | + |
| 253 | + Hrad = math.radians(H) |
| 254 | + U = (math.cos(Hrad) * C) |
| 255 | + V = (math.sin(Hrad) * C) |
| 256 | + |
| 257 | + return [L, U, V] |
| 258 | + |
| 259 | + |
| 260 | +def husl_to_lch(triple): |
| 261 | + H, S, L = triple |
| 262 | + |
| 263 | + if L > 99.9999999: |
| 264 | + return [100, 0.0, H] |
| 265 | + if L < 0.00000001: |
| 266 | + return [0.0, 0.0, H] |
| 267 | + |
| 268 | + mx = max_chroma(L, H) |
| 269 | + C = mx / 100.0 * S |
| 270 | + |
| 271 | + return [L, C, H] |
| 272 | + |
| 273 | + |
| 274 | +def lch_to_husl(triple): |
| 275 | + L, C, H = triple |
| 276 | + |
| 277 | + if L > 99.9999999: |
| 278 | + return [H, 0.0, 100.0] |
| 279 | + if L < 0.00000001: |
| 280 | + return [H, 0.0, 0.0] |
| 281 | + |
| 282 | + mx = max_chroma(L, H) |
| 283 | + S = C / mx * 100.0 |
| 284 | + |
| 285 | + return [H, S, L] |
| 286 | + |
| 287 | + |
| 288 | +def huslp_to_lch(triple): |
| 289 | + H, S, L = triple |
| 290 | + |
| 291 | + if L > 99.9999999: |
| 292 | + return [100, 0.0, H] |
| 293 | + if L < 0.00000001: |
| 294 | + return [0.0, 0.0, H] |
| 295 | + |
| 296 | + mx = max_chroma_pastel(L) |
| 297 | + C = mx / 100.0 * S |
| 298 | + |
| 299 | + return [L, C, H] |
| 300 | + |
| 301 | + |
| 302 | +def lch_to_huslp(triple): |
| 303 | + L, C, H = triple |
| 304 | + |
| 305 | + if L > 99.9999999: |
| 306 | + return [H, 0.0, 100.0] |
| 307 | + if L < 0.00000001: |
| 308 | + return [H, 0.0, 0.0] |
| 309 | + |
| 310 | + mx = max_chroma_pastel(L) |
| 311 | + S = C / mx * 100.0 |
| 312 | + |
| 313 | + return [H, S, L] |
0 commit comments