| 1 |
# Copyright 2014 Tom Rothamel <[email protected]> |
| 2 |
# Copyright 2014 Patrick Dawson <[email protected]> |
| 3 |
# |
| 4 |
# This software is provided 'as-is', without any express or implied |
| 5 |
# warranty. In no event will the authors be held liable for any damages |
| 6 |
# arising from the use of this software. |
| 7 |
# |
| 8 |
# Permission is granted to anyone to use this software for any purpose, |
| 9 |
# including commercial applications, and to alter it and redistribute it |
| 10 |
# freely, subject to the following restrictions: |
| 11 |
# |
| 12 |
# 1. The origin of this software must not be misrepresented; you must not |
| 13 |
# claim that you wrote the original software. If you use this software |
| 14 |
# in a product, an acknowledgment in the product documentation would be |
| 15 |
# appreciated but is not required. |
| 16 |
# 2. Altered source versions must be plainly marked as such, and must not be |
| 17 |
# misrepresented as being the original software. |
| 18 |
# 3. This notice may not be removed or altered from any source distribution. |
| 19 |
|
| 20 |
from sdl2 cimport * |
| 21 |
import binascii |
| 22 |
import struct |
| 23 |
|
| 24 |
include "color_dict.pxi" |
| 25 |
|
| 26 |
cdef Uint32 map_color(SDL_Surface *surface, color) except? 0xaabbccdd: |
| 27 |
""" |
| 28 |
Maps `color` into an RGBA color value that can be used with `surface`. |
| 29 |
""" |
| 30 |
|
| 31 |
cdef Uint8 r, g, b, a |
| 32 |
|
| 33 |
if isinstance(color, (tuple, list, Color)) and len(color) == 4: |
| 34 |
r, g, b, a = color |
| 35 |
elif isinstance(color, (tuple, list, Color)) and len(color) == 3: |
| 36 |
r, g, b = color |
| 37 |
a = 255 |
| 38 |
elif isinstance(color, int): |
| 39 |
return color |
| 40 |
else: |
| 41 |
raise TypeError("Expected a color.") |
| 42 |
|
| 43 |
return SDL_MapRGBA(surface.format, r, g, b, a) |
| 44 |
|
| 45 |
cdef object get_color(Uint32 pixel, SDL_Surface *surface): |
| 46 |
cdef Uint8 r |
| 47 |
cdef Uint8 g |
| 48 |
cdef Uint8 b |
| 49 |
cdef Uint8 a |
| 50 |
|
| 51 |
SDL_GetRGBA(pixel, surface.format, &r, &g, &b, &a) |
| 52 |
|
| 53 |
return Color(r, g, b, a) |
| 54 |
|
| 55 |
cdef to_sdl_color(color, SDL_Color *out): |
| 56 |
if not isinstance(color, Color): |
| 57 |
color = Color(color) |
| 58 |
out.r = color.r |
| 59 |
out.g = color.g |
| 60 |
out.b = color.b |
| 61 |
out.a = color.a |
| 62 |
|
| 63 |
cdef class Color: |
| 64 |
cdef from_rgba(self, Uint8 r, Uint8 g, Uint8 b, Uint8 a): |
| 65 |
self.r = r |
| 66 |
self.g = g |
| 67 |
self.b = b |
| 68 |
self.a = a |
| 69 |
|
| 70 |
cdef from_hex(self, c): |
| 71 |
# Handle short hex strings. |
| 72 |
if len(c) == 3 or len(c) == 4: |
| 73 |
c = "".join(map(lambda x: x*2, c)) |
| 74 |
|
| 75 |
try: |
| 76 |
if len(c) == 6: |
| 77 |
r, g, b = struct.unpack('BBB', binascii.unhexlify(c)) |
| 78 |
a = 255 |
| 79 |
elif len(c) == 8: |
| 80 |
r, g, b, a = struct.unpack('BBBB', binascii.unhexlify(c)) |
| 81 |
else: |
| 82 |
raise ValueError(c) |
| 83 |
except TypeError as e: |
| 84 |
raise ValueError(c) |
| 85 |
|
| 86 |
self.from_rgba(r, g, b, a) |
| 87 |
|
| 88 |
cdef from_name(self, c): |
| 89 |
# Remove all whitespace. |
| 90 |
c = "".join(c.split()).lower() |
| 91 |
|
| 92 |
try: |
| 93 |
r, g, b = colors[c] |
| 94 |
except KeyError as e: |
| 95 |
raise ValueError(c) |
| 96 |
self.from_rgba(r, g, b, 255) |
| 97 |
|
| 98 |
def __richcmp__(Color x, y, int op): |
| 99 |
if op == 3: |
| 100 |
return not (x == y) |
| 101 |
|
| 102 |
if isinstance(y, tuple): |
| 103 |
y = Color(y) |
| 104 |
if not isinstance(y, Color): |
| 105 |
return False |
| 106 |
if op == 2: |
| 107 |
return x.r == y.r and x.g == y.g and x.b == y.b and x.a == y.a |
| 108 |
|
| 109 |
def __cinit__(self): |
| 110 |
self.length = 4 |
| 111 |
self.r = 0 |
| 112 |
self.g = 0 |
| 113 |
self.b = 0 |
| 114 |
self.a = 255 |
| 115 |
|
| 116 |
def __init__(self, *args): |
| 117 |
self.length = 4 |
| 118 |
|
| 119 |
if len(args) == 1: |
| 120 |
c = args[0] |
| 121 |
if isinstance(c, basestring): |
| 122 |
if c.startswith('#'): |
| 123 |
self.from_hex(c[1:]) |
| 124 |
elif c.startswith('0x'): |
| 125 |
self.from_hex(c[2:]) |
| 126 |
else: |
| 127 |
self.from_name(c) |
| 128 |
elif isinstance(c, (tuple, list, Color)): |
| 129 |
if len(c) == 4: |
| 130 |
self.from_rgba(c[0], c[1], c[2], c[3]) |
| 131 |
elif len(c) == 3: |
| 132 |
self.from_rgba(c[0], c[1], c[2], 255) |
| 133 |
else: |
| 134 |
raise ValueError(c) |
| 135 |
else: |
| 136 |
self.from_hex("%08x" % c) |
| 137 |
|
| 138 |
elif len(args) == 3: |
| 139 |
r, g, b = args |
| 140 |
self.from_rgba(r, g, b, 255) |
| 141 |
elif len(args) == 4: |
| 142 |
r, g, b, a = args |
| 143 |
self.from_rgba(r, g, b, a) |
| 144 |
|
| 145 |
def __repr__(self): |
| 146 |
return str((self.r, self.g, self.b, self.a)) |
| 147 |
|
| 148 |
def __int__(self): |
| 149 |
packed = struct.pack('BBBB', self.r, self.g, self.b, self.a) |
| 150 |
return struct.unpack('>L', packed)[0] |
| 151 |
|
| 152 |
def __hex__(self): |
| 153 |
return hex(int(self)) |
| 154 |
|
| 155 |
def __oct__(self): |
| 156 |
return oct(int(self)) |
| 157 |
|
| 158 |
def __float__(self): |
| 159 |
return float(int(self)) |
| 160 |
|
| 161 |
def __reduce__(self): |
| 162 |
d = {} |
| 163 |
d['rgba'] = (self.r, self.g, self.b, self.a) |
| 164 |
return (Color, (), d) |
| 165 |
|
| 166 |
def __setstate__(self, d): |
| 167 |
self.r, self.g, self.b, self.a = d['rgba'] |
| 168 |
|
| 169 |
def __setitem__(self, key, val): |
| 170 |
if not isinstance(val, int): |
| 171 |
raise ValueError(val) |
| 172 |
if key >= self.length: |
| 173 |
raise IndexError(key) |
| 174 |
if val < 0 or val > 255: |
| 175 |
raise ValueError(val) |
| 176 |
|
| 177 |
if key == 0: self.r = val |
| 178 |
elif key == 1: self.g = val |
| 179 |
elif key == 2: self.b = val |
| 180 |
elif key == 3: self.a = val |
| 181 |
else: |
| 182 |
raise IndexError(key) |
| 183 |
|
| 184 |
def __getitem__(self, key): |
| 185 |
if isinstance(key, slice): |
| 186 |
return tuple(self)[key] |
| 187 |
if key >= self.length: |
| 188 |
raise IndexError(key) |
| 189 |
if key == 0: return self.r |
| 190 |
elif key == 1: return self.g |
| 191 |
elif key == 2: return self.b |
| 192 |
elif key == 3: return self.a |
| 193 |
else: |
| 194 |
raise IndexError(key) |
| 195 |
|
| 196 |
def __len__(self): |
| 197 |
return self.length |
| 198 |
|
| 199 |
def __mul__(self not None, Color rhs not None): |
| 200 |
# Multiplying this way doesn't make much sense, |
| 201 |
# but it's how pygame does it. |
| 202 |
|
| 203 |
r = min(255, self.r * rhs.r) |
| 204 |
g = min(255, self.g * rhs.g) |
| 205 |
b = min(255, self.b * rhs.b) |
| 206 |
a = min(255, self.a * rhs.a) |
| 207 |
|
| 208 |
return type(self)(r, g, b, a) |
| 209 |
|
| 210 |
def __add__(self not None, Color rhs not None): |
| 211 |
r = min(255, self.r + rhs.r) |
| 212 |
g = min(255, self.g + rhs.g) |
| 213 |
b = min(255, self.b + rhs.b) |
| 214 |
a = min(255, self.a + rhs.a) |
| 215 |
|
| 216 |
return type(self)(r, g, b, a) |
| 217 |
|
| 218 |
def __sub__(self not None, Color rhs not None): |
| 219 |
r = max(0, self.r - rhs.r) |
| 220 |
g = max(0, self.g - rhs.g) |
| 221 |
b = max(0, self.b - rhs.b) |
| 222 |
a = max(0, self.a - rhs.a) |
| 223 |
|
| 224 |
return type(self)(r, g, b, a) |
| 225 |
|
| 226 |
def __mod__(self not None, Color rhs not None): |
| 227 |
|
| 228 |
def mod(l, r): |
| 229 |
if r == 0: |
| 230 |
return 0 |
| 231 |
|
| 232 |
return l % r |
| 233 |
|
| 234 |
r = mod(self.r, rhs.r) |
| 235 |
g = mod(self.g, rhs.g) |
| 236 |
b = mod(self.b, rhs.b) |
| 237 |
a = mod(self.a, rhs.a) |
| 238 |
|
| 239 |
return type(self)(r, g, b, a) |
| 240 |
|
| 241 |
def __div__(self not None, Color rhs not None): |
| 242 |
r = min(255, self.r / rhs.r) |
| 243 |
g = min(255, self.g / rhs.g) |
| 244 |
b = min(255, self.b / rhs.b) |
| 245 |
a = min(255, self.a / rhs.a) |
| 246 |
|
| 247 |
return type(self)(r, g, b, a) |
| 248 |
|
| 249 |
def __floordiv__(self not None, Color rhs not None): |
| 250 |
|
| 251 |
def div(a, b): |
| 252 |
if b == 0: |
| 253 |
return b |
| 254 |
|
| 255 |
return min(255, a // b) |
| 256 |
|
| 257 |
r = div(self.r, rhs.r) |
| 258 |
g = div(self.g, rhs.g) |
| 259 |
b = div(self.b, rhs.b) |
| 260 |
a = div(self.a, rhs.a) |
| 261 |
|
| 262 |
return type(self)(r, g, b, a) |
| 263 |
|
| 264 |
property cmy: |
| 265 |
def __get__(self): |
| 266 |
return 1 - (self.r / 255.0), 1 - (self.g / 255.0), 1 - (self.b / 255.0) |
| 267 |
|
| 268 |
def __set__(self, val): |
| 269 |
c, m, y = val |
| 270 |
self.r = (1 - c) * 255 |
| 271 |
self.g = (1 - m) * 255 |
| 272 |
self.b = (1 - y) * 255 |
| 273 |
|
| 274 |
property hsva: |
| 275 |
def __get__(self): |
| 276 |
cdef double r = self.r / 255.0 |
| 277 |
cdef double g = self.g / 255.0 |
| 278 |
cdef double b = self.b / 255.0 |
| 279 |
|
| 280 |
cdef double cmax = max(r, g, b) |
| 281 |
cdef double cmin = min(r, g, b) |
| 282 |
cdef double delta = cmax - cmin |
| 283 |
|
| 284 |
cdef double h, s, v, a |
| 285 |
|
| 286 |
if r == g == b: |
| 287 |
h = 0.0 |
| 288 |
s = 0.0 |
| 289 |
else: |
| 290 |
if cmax == r: |
| 291 |
h = 60.0 * ((g - b) / delta % 6) |
| 292 |
elif cmax == g: |
| 293 |
h = 60.0 * ((b - r) / delta + 2) |
| 294 |
else: |
| 295 |
h = 60.0 * ((r - g) / delta + 4) |
| 296 |
|
| 297 |
if cmax == 0.0: |
| 298 |
s = 0.0 |
| 299 |
else: |
| 300 |
s = delta / cmax * 100 |
| 301 |
|
| 302 |
v = cmax * 100 |
| 303 |
a = self.a / 255.0 * 100 |
| 304 |
return h, s, v, a |
| 305 |
|
| 306 |
def __set__(self, val): |
| 307 |
cdef double h, s, v, a |
| 308 |
if len(val) == 3: |
| 309 |
h, s, v = val |
| 310 |
a = 0.0 |
| 311 |
else: |
| 312 |
h, s, v, a = val |
| 313 |
|
| 314 |
h = h % 360.0 |
| 315 |
|
| 316 |
# These should be in a range of [0.0, 1.0] |
| 317 |
s /= 100.0 |
| 318 |
v /= 100.0 |
| 319 |
a /= 100.0 |
| 320 |
|
| 321 |
cdef double c = v * s |
| 322 |
cdef double x = c * (1 - abs((h / 60.0) % 2 - 1)) |
| 323 |
cdef double m = v - c |
| 324 |
|
| 325 |
cdef double r, g, b |
| 326 |
|
| 327 |
if 0 <= h < 60: |
| 328 |
r, g, b = c, x, 0 |
| 329 |
elif 60 <= h < 120: |
| 330 |
r, g, b = x, c, 0 |
| 331 |
elif 120 <= h < 180: |
| 332 |
r, g, b = 0, c, x |
| 333 |
elif 180 <= h < 240: |
| 334 |
r, g, b = 0, x, c |
| 335 |
elif 240 <= h < 300: |
| 336 |
r, g, b = x, 0, c |
| 337 |
elif 300 <= h < 360: |
| 338 |
r, g, b = c, 0, x |
| 339 |
else: |
| 340 |
raise ValueError() |
| 341 |
|
| 342 |
self.r = int(255 * (r + m)) |
| 343 |
self.g = int(255 * (g + m)) |
| 344 |
self.b = int(255 * (b + m)) |
| 345 |
self.a = int(255 * a) |
| 346 |
|
| 347 |
property hsla: |
| 348 |
def __get__(self): |
| 349 |
cdef double h, s, l, a |
| 350 |
cdef double r, g, b |
| 351 |
cdef double cmin, cmax, delta |
| 352 |
|
| 353 |
h = self.hsva[0] % 360.0 |
| 354 |
|
| 355 |
r = self.r / 255.0 |
| 356 |
g = self.g / 255.0 |
| 357 |
b = self.b / 255.0 |
| 358 |
|
| 359 |
cmin = min(r, g, b) |
| 360 |
cmax = max(r, g, b) |
| 361 |
delta = cmax - cmin |
| 362 |
|
| 363 |
l = (cmax + cmin) / 2.0 |
| 364 |
|
| 365 |
if delta == 0: |
| 366 |
s = 0.0 |
| 367 |
else: |
| 368 |
s = delta / (1 - abs(2 * l - 1)) |
| 369 |
|
| 370 |
a = self.a / 255.0 * 100 |
| 371 |
|
| 372 |
s = min(100.0, s * 100) |
| 373 |
l = min(100.0, l * 100) |
| 374 |
|
| 375 |
return h, s, l, a |
| 376 |
|
| 377 |
def __set__(self, val): |
| 378 |
cdef double h, s, l, a |
| 379 |
if len(val) == 3: |
| 380 |
h, s, l = val |
| 381 |
a = 0.0 |
| 382 |
else: |
| 383 |
h, s, l, a = val |
| 384 |
|
| 385 |
s /= 100.0 |
| 386 |
l /= 100.0 |
| 387 |
a /= 100.0 |
| 388 |
|
| 389 |
cdef double c = (1 - abs(2*l - 1)) * s |
| 390 |
cdef double x = c * (1 - abs((h / 60.0) % 2 - 1)) |
| 391 |
cdef double m = l - c / 2.0 |
| 392 |
|
| 393 |
cdef double r, g, b |
| 394 |
if 0 <= h < 60: |
| 395 |
r, g, b = c, x, 0 |
| 396 |
elif 60 <= h < 120: |
| 397 |
r, g, b = x, c, 0 |
| 398 |
elif 120 <= h < 180: |
| 399 |
r, g, b = 0, c, x |
| 400 |
elif 180 <= h < 240: |
| 401 |
r, g, b = 0, x, c |
| 402 |
elif 240 <= h < 300: |
| 403 |
r, g, b = x, 0, c |
| 404 |
elif 300 <= h < 360: |
| 405 |
r, g, b = c, 0, x |
| 406 |
else: |
| 407 |
raise ValueError() |
| 408 |
|
| 409 |
self.r = int(255 * (r + m)) |
| 410 |
self.g = int(255 * (g + m)) |
| 411 |
self.b = int(255 * (b + m)) |
| 412 |
self.a = int(255 * a) |
| 413 |
|
| 414 |
property i1i2i3: |
| 415 |
def __get__(self): |
| 416 |
# Take the dot product as described here: |
| 417 |
# http://de.wikipedia.org/wiki/I1I2I3-Farbraum |
| 418 |
|
| 419 |
cdef double i1, i2, i3 |
| 420 |
cdef double r, g, b |
| 421 |
|
| 422 |
r = self.r / 255.0 |
| 423 |
g = self.g / 255.0 |
| 424 |
b = self.b / 255.0 |
| 425 |
|
| 426 |
i1 = (r + g + b) / 3.0 |
| 427 |
i2 = (r - b) / 2.0 |
| 428 |
i3 = (2*g - r - b) / 4.0 |
| 429 |
|
| 430 |
return i1, i2, i3 |
| 431 |
|
| 432 |
def __set__(self, val): |
| 433 |
# Dot product with the inverted matrix. |
| 434 |
|
| 435 |
cdef double i1, i2, i3 |
| 436 |
cdef double r, g, b |
| 437 |
|
| 438 |
i1, i2, i3 = val |
| 439 |
|
| 440 |
r = i1 + i2 - (2.0/3.0 * i3) |
| 441 |
g = i1 + (4.0/3.0 * i3) |
| 442 |
b = i1 - i2 - (2.0/3.0 * i3) |
| 443 |
|
| 444 |
# Don't change alpha. |
| 445 |
self.r = int(r * 255) |
| 446 |
self.g = int(g * 255) |
| 447 |
self.b = int(b * 255) |
| 448 |
|
| 449 |
def normalize(self): |
| 450 |
return self.r / 255.0, self.g / 255.0, self.b / 255.0, self.a / 255.0 |
| 451 |
|
| 452 |
def correct_gamma(self, gamma): |
| 453 |
m = map(lambda x: int(round(pow(x / 255.0, gamma) * 255)), tuple(self)) |
| 454 |
c = type(self)(tuple(m)) |
| 455 |
return c |
| 456 |
|
| 457 |
def set_length(self, n): |
| 458 |
if n > 4 or n < 1: |
| 459 |
raise ValueError(n) |
| 460 |
self.length = n |