| 1 |
/* |
| 2 |
* Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2026 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 05 нояб. 2015 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/runtime/Color.h> |
| 23 |
#include <lsp-plug.in/stdlib/locale.h> |
| 24 |
#include <lsp-plug.in/stdlib/math.h> |
| 25 |
#include <lsp-plug.in/stdlib/stdio.h> |
| 26 |
#include <lsp-plug.in/stdlib/string.h> |
| 27 |
|
| 28 |
#include <ctype.h> |
| 29 |
#include <errno.h> |
| 30 |
|
| 31 |
namespace lsp |
| 32 |
{ |
| 33 |
static constexpr float HSL_RGB_1_3 = 1.0f / 3.0f; |
| 34 |
static constexpr float HSL_RGB_1_6 = 1.0f / 6.0f; |
| 35 |
static constexpr float HSL_RGB_2_3 = 2.0f / 3.0f; |
| 36 |
static constexpr float FLOAT_COL_COEFF = 255.0f; |
| 37 |
static constexpr float FLOAT_COL_BIAS = 0.25f; |
| 38 |
static constexpr float FLOAT_RCOL_COEFF = 1.0f / FLOAT_COL_COEFF; |
| 39 |
static constexpr float DEG_TO_RAD = M_PI / 180.0f; |
| 40 |
static constexpr float RAD_TO_DEG = 180.0f / M_PI; |
| 41 |
static constexpr float ONE_DIV_360 = 1.0f / 360.0f; |
| 42 |
static constexpr float ONE_DIV_116 = 1.0f / 116.0f; |
| 43 |
static constexpr float ONE_DIV_3 = 1.0f / 3.0f; |
| 44 |
static constexpr float ONE_DIV_2p4 = 1.0f / 2.4f; |
| 45 |
static constexpr float ONE_DIV_1p055 = 1.0f / 1.055f; |
| 46 |
static constexpr float ONE_DIV_12p92 = 1.0f / 12.92f; |
| 47 |
static constexpr float DIV_16_BY_116 = 16.0f / 116.0f; |
| 48 |
static constexpr float DIV_ONE_BY_7p787 = 1.0f / 7.787f; |
| 49 |
static constexpr float ONE_DIV_95p047 = 1.0f / 95.047f; |
| 50 |
static constexpr float ONE_DIV_108p883 = 1.0f / 108.883f; |
| 51 |
|
| 52 |
static const char *skip_whitespace(const char *src, const char *end) noexcept |
| 53 |
{ |
| 54 |
for ( ; src < end; ++src) |
| 55 |
{ |
| 56 |
switch (*src) |
| 57 |
{ |
| 58 |
case '\n': |
| 59 |
case '\t': |
| 60 |
case '\r': |
| 61 |
case ' ': |
| 62 |
break; |
| 63 |
case '\0': |
| 64 |
return end; |
| 65 |
default: |
| 66 |
return src; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return src; |
| 71 |
} |
| 72 |
|
| 73 |
static const char *match_prefix(const char *src, const char *end, const char *prefix) noexcept |
| 74 |
{ |
| 75 |
for ( ; src < end; ++src) |
| 76 |
{ |
| 77 |
if (*prefix == '\0') |
| 78 |
return src; |
| 79 |
if (tolower(*src) != *(prefix++)) |
| 80 |
return end; |
| 81 |
} |
| 82 |
return src; |
| 83 |
} |
| 84 |
|
| 85 |
static const char *match_char(const char *src, const char *end, char ch) noexcept |
| 86 |
{ |
| 87 |
if (src < end) |
| 88 |
return (src[0] == ch) ? &src[1] : end; |
| 89 |
return end; |
| 90 |
} |
| 91 |
|
| 92 |
static const char *match_chars(const char *src, const char *end, const char *allowed) noexcept |
| 93 |
{ |
| 94 |
for ( ; src < end; ++src) |
| 95 |
{ |
| 96 |
char c = *src; |
| 97 |
if (c == '\0') |
| 98 |
return src; |
| 99 |
|
| 100 |
// Lookup list of allowed characters |
| 101 |
for (const char *ck = allowed; *ck != '\0'; ++ck) |
| 102 |
{ |
| 103 |
if (*ck == c) |
| 104 |
return src; |
| 105 |
} |
| 106 |
} |
| 107 |
return src; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
static size_t hex_char(char c) noexcept |
| 112 |
{ |
| 113 |
if ((c >= '0') && (c <= '9')) |
| 114 |
return c - '0'; |
| 115 |
if ((c >= 'a') && (c <= 'f')) |
| 116 |
return c - 'a' + 10; |
| 117 |
if ((c >= 'A') && (c <= 'F')) |
| 118 |
return c - 'A' + 10; |
| 119 |
return 0xff; |
| 120 |
} |
| 121 |
|
| 122 |
static const char *skip_hex(const char *ptr, const char *end) noexcept |
| 123 |
{ |
| 124 |
for ( ; ptr < end; ++ptr) |
| 125 |
{ |
| 126 |
if (hex_char(*ptr) >= 16) |
| 127 |
return ptr; |
| 128 |
} |
| 129 |
return end; |
| 130 |
} |
| 131 |
|
| 132 |
inline float Color::clamp(float x) noexcept |
| 133 |
{ |
| 134 |
return lsp_limit(x, 0.0f, 1.0f); |
| 135 |
} |
| 136 |
|
| 137 |
Color::Color() noexcept |
| 138 |
{ |
| 139 |
rgb.R = 0.0f; |
| 140 |
rgb.G = 0.0f; |
| 141 |
rgb.B = 0.0f; |
| 142 |
hsl.H = 0.0f; |
| 143 |
hsl.S = 0.0f; |
| 144 |
hsl.L = 0.0f; |
| 145 |
xyz.X = 0.0f; |
| 146 |
xyz.Y = 0.0f; |
| 147 |
xyz.Z = 0.0f; |
| 148 |
lab.L = 0.0f; |
| 149 |
lab.A = 0.0f; |
| 150 |
lab.B = 0.0f; |
| 151 |
lch.L = 0.0f; |
| 152 |
lch.C = 0.0f; |
| 153 |
lch.H = 0.0f; |
| 154 |
cmyk.C = 0.0f; |
| 155 |
cmyk.M = 0.0f; |
| 156 |
cmyk.Y = 0.0f; |
| 157 |
cmyk.K = 0.0f; |
| 158 |
mask = M_RGB; |
| 159 |
A = 0.0f; |
| 160 |
} |
| 161 |
|
| 162 |
Color::Color(float r, float g, float b) noexcept |
| 163 |
{ |
| 164 |
set_rgb(r, g, b); |
| 165 |
hsl.H = 0.0f; |
| 166 |
hsl.S = 0.0f; |
| 167 |
hsl.L = 0.0f; |
| 168 |
xyz.X = 0.0f; |
| 169 |
xyz.Y = 0.0f; |
| 170 |
xyz.Z = 0.0f; |
| 171 |
lab.L = 0.0f; |
| 172 |
lab.A = 0.0f; |
| 173 |
lab.B = 0.0f; |
| 174 |
lch.L = 0.0f; |
| 175 |
lch.C = 0.0f; |
| 176 |
lch.H = 0.0f; |
| 177 |
cmyk.C = 0.0f; |
| 178 |
cmyk.M = 0.0f; |
| 179 |
cmyk.Y = 0.0f; |
| 180 |
cmyk.K = 0.0f; |
| 181 |
A = 0.0f; |
| 182 |
} |
| 183 |
|
| 184 |
Color::Color(float r, float g, float b, float a) noexcept |
| 185 |
{ |
| 186 |
set_rgba(r, g, b, a); |
| 187 |
hsl.H = 0.0f; |
| 188 |
hsl.S = 0.0f; |
| 189 |
hsl.L = 0.0f; |
| 190 |
xyz.X = 0.0f; |
| 191 |
xyz.Y = 0.0f; |
| 192 |
xyz.Z = 0.0f; |
| 193 |
lab.L = 0.0f; |
| 194 |
lab.A = 0.0f; |
| 195 |
lab.B = 0.0f; |
| 196 |
lch.L = 0.0f; |
| 197 |
lch.C = 0.0f; |
| 198 |
lch.H = 0.0f; |
| 199 |
cmyk.C = 0.0f; |
| 200 |
cmyk.M = 0.0f; |
| 201 |
cmyk.Y = 0.0f; |
| 202 |
cmyk.K = 0.0f; |
| 203 |
} |
| 204 |
|
| 205 |
Color::Color(const Color &src) noexcept |
| 206 |
{ |
| 207 |
rgb = src.rgb; |
| 208 |
hsl = src.hsl; |
| 209 |
xyz = src.xyz; |
| 210 |
lab = src.lab; |
| 211 |
lch = src.lch; |
| 212 |
cmyk = src.cmyk; |
| 213 |
mask = src.mask; |
| 214 |
A = src.A; |
| 215 |
} |
| 216 |
|
| 217 |
Color::Color(Color &&src) noexcept |
| 218 |
{ |
| 219 |
rgb = src.rgb; |
| 220 |
hsl = src.hsl; |
| 221 |
xyz = src.xyz; |
| 222 |
lab = src.lab; |
| 223 |
lch = src.lch; |
| 224 |
cmyk = src.cmyk; |
| 225 |
mask = src.mask; |
| 226 |
A = src.A; |
| 227 |
} |
| 228 |
|
| 229 |
Color &Color::operator = (const Color & src) noexcept |
| 230 |
{ |
| 231 |
rgb = src.rgb; |
| 232 |
hsl = src.hsl; |
| 233 |
xyz = src.xyz; |
| 234 |
lab = src.lab; |
| 235 |
lch = src.lch; |
| 236 |
cmyk = src.cmyk; |
| 237 |
mask = src.mask; |
| 238 |
A = src.A; |
| 239 |
|
| 240 |
return *this; |
| 241 |
} |
| 242 |
|
| 243 |
Color &Color::operator = (Color && src) noexcept |
| 244 |
{ |
| 245 |
rgb = src.rgb; |
| 246 |
hsl = src.hsl; |
| 247 |
xyz = src.xyz; |
| 248 |
lab = src.lab; |
| 249 |
lch = src.lch; |
| 250 |
cmyk = src.cmyk; |
| 251 |
mask = src.mask; |
| 252 |
A = src.A; |
| 253 |
|
| 254 |
return *this; |
| 255 |
} |
| 256 |
|
| 257 |
Color::Color(const Color &src, float a) noexcept |
| 258 |
{ |
| 259 |
rgb = src.rgb; |
| 260 |
hsl = src.hsl; |
| 261 |
xyz = src.xyz; |
| 262 |
lab = src.lab; |
| 263 |
lch = src.lch; |
| 264 |
cmyk = src.cmyk; |
| 265 |
mask = src.mask; |
| 266 |
A = clamp(a); |
| 267 |
} |
| 268 |
|
| 269 |
Color::Color(const Color *src) noexcept |
| 270 |
{ |
| 271 |
rgb = src->rgb; |
| 272 |
hsl = src->hsl; |
| 273 |
xyz = src->xyz; |
| 274 |
lab = src->lab; |
| 275 |
lch = src->lch; |
| 276 |
cmyk = src->cmyk; |
| 277 |
mask = src->mask; |
| 278 |
A = src->A; |
| 279 |
} |
| 280 |
|
| 281 |
Color::Color(const Color *src, float a) noexcept |
| 282 |
{ |
| 283 |
rgb = src->rgb; |
| 284 |
hsl = src->hsl; |
| 285 |
xyz = src->xyz; |
| 286 |
lab = src->lab; |
| 287 |
lch = src->lch; |
| 288 |
cmyk = src->cmyk; |
| 289 |
mask = src->mask; |
| 290 |
A = clamp(a); |
| 291 |
} |
| 292 |
|
| 293 |
Color::Color(uint32_t rgb) noexcept |
| 294 |
{ |
| 295 |
set_rgb24(rgb); |
| 296 |
hsl.H = 0.0f; |
| 297 |
hsl.S = 0.0f; |
| 298 |
hsl.L = 0.0f; |
| 299 |
xyz.X = 0.0f; |
| 300 |
xyz.Y = 0.0f; |
| 301 |
xyz.Z = 0.0f; |
| 302 |
lab.L = 0.0f; |
| 303 |
lab.A = 0.0f; |
| 304 |
lab.B = 0.0f; |
| 305 |
lch.L = 0.0f; |
| 306 |
lch.C = 0.0f; |
| 307 |
lch.H = 0.0f; |
| 308 |
cmyk.C = 0.0f; |
| 309 |
cmyk.M = 0.0f; |
| 310 |
cmyk.Y = 0.0f; |
| 311 |
cmyk.K = 0.0f; |
| 312 |
} |
| 313 |
|
| 314 |
Color::Color(uint32_t rgb, float a) noexcept |
| 315 |
{ |
| 316 |
set_rgb24(rgb); |
| 317 |
hsl.H = 0.0f; |
| 318 |
hsl.S = 0.0f; |
| 319 |
hsl.L = 0.0f; |
| 320 |
xyz.X = 0.0f; |
| 321 |
xyz.Y = 0.0f; |
| 322 |
xyz.Z = 0.0f; |
| 323 |
lab.L = 0.0f; |
| 324 |
lab.A = 0.0f; |
| 325 |
lab.B = 0.0f; |
| 326 |
lch.L = 0.0f; |
| 327 |
lch.C = 0.0f; |
| 328 |
lch.H = 0.0f; |
| 329 |
cmyk.C = 0.0f; |
| 330 |
cmyk.M = 0.0f; |
| 331 |
cmyk.Y = 0.0f; |
| 332 |
cmyk.K = 0.0f; |
| 333 |
A = a; |
| 334 |
} |
| 335 |
|
| 336 |
Color &Color::copy(const Color &c) noexcept |
| 337 |
{ |
| 338 |
rgb = c.rgb; |
| 339 |
hsl = c.hsl; |
| 340 |
xyz = c.xyz; |
| 341 |
lab = c.lab; |
| 342 |
cmyk = c.cmyk; |
| 343 |
A = c.A; |
| 344 |
mask = c.mask; |
| 345 |
|
| 346 |
return *this; |
| 347 |
} |
| 348 |
|
| 349 |
Color &Color::copy(const Color *c) noexcept |
| 350 |
{ |
| 351 |
rgb = c->rgb; |
| 352 |
hsl = c->hsl; |
| 353 |
xyz = c->xyz; |
| 354 |
lab = c->lab; |
| 355 |
lch = c->lch; |
| 356 |
cmyk = c->cmyk; |
| 357 |
A = c->A; |
| 358 |
mask = c->mask; |
| 359 |
|
| 360 |
return *this; |
| 361 |
} |
| 362 |
|
| 363 |
Color &Color::copy(const Color &c, float a) noexcept |
| 364 |
{ |
| 365 |
rgb = c.rgb; |
| 366 |
hsl = c.hsl; |
| 367 |
xyz = c.xyz; |
| 368 |
lab = c.lab; |
| 369 |
lch = c.lch; |
| 370 |
cmyk = c.cmyk; |
| 371 |
A = clamp(a); |
| 372 |
mask = c.mask; |
| 373 |
|
| 374 |
return *this; |
| 375 |
} |
| 376 |
|
| 377 |
Color &Color::copy(const Color *c, float a) noexcept |
| 378 |
{ |
| 379 |
rgb = c->rgb; |
| 380 |
hsl = c->hsl; |
| 381 |
xyz = c->xyz; |
| 382 |
lab = c->lab; |
| 383 |
lch = c->lch; |
| 384 |
cmyk = c->cmyk; |
| 385 |
A = clamp(a); |
| 386 |
mask = c->mask; |
| 387 |
|
| 388 |
return *this; |
| 389 |
} |
| 390 |
|
| 391 |
void Color::swap(Color *c) noexcept |
| 392 |
{ |
| 393 |
lsp::swap(rgb, c->rgb); |
| 394 |
lsp::swap(hsl, c->hsl); |
| 395 |
lsp::swap(xyz, c->xyz); |
| 396 |
lsp::swap(lab, c->lab); |
| 397 |
lsp::swap(lch, c->lch); |
| 398 |
lsp::swap(cmyk, c->cmyk); |
| 399 |
lsp::swap(A, c->A); |
| 400 |
lsp::swap(mask, c->mask); |
| 401 |
} |
| 402 |
|
| 403 |
Color &Color::red(float r) noexcept |
| 404 |
{ |
| 405 |
calc_rgb().R = clamp(r); |
| 406 |
mask = M_RGB; |
| 407 |
return *this; |
| 408 |
} |
| 409 |
|
| 410 |
Color &Color::green(float g) noexcept |
| 411 |
{ |
| 412 |
calc_rgb().G = clamp(g); |
| 413 |
mask = M_RGB; |
| 414 |
return *this; |
| 415 |
} |
| 416 |
|
| 417 |
Color &Color::blue(float b) noexcept |
| 418 |
{ |
| 419 |
calc_rgb().B = clamp(b); |
| 420 |
mask = M_RGB; |
| 421 |
return *this; |
| 422 |
} |
| 423 |
|
| 424 |
Color &Color::hue(float h) noexcept |
| 425 |
{ |
| 426 |
calc_hsl().H = clamp(h); |
| 427 |
mask = M_HSL; |
| 428 |
return *this; |
| 429 |
} |
| 430 |
|
| 431 |
Color &Color::saturation(float s) noexcept |
| 432 |
{ |
| 433 |
calc_hsl().S = clamp(s); |
| 434 |
mask = M_HSL; |
| 435 |
return *this; |
| 436 |
} |
| 437 |
|
| 438 |
Color &Color::lightness(float l) noexcept |
| 439 |
{ |
| 440 |
calc_hsl().L = clamp(l); |
| 441 |
mask = M_HSL; |
| 442 |
return *this; |
| 443 |
} |
| 444 |
|
| 445 |
Color &Color::hsl_hue(float h) noexcept |
| 446 |
{ |
| 447 |
calc_hsl().H = clamp(h); |
| 448 |
mask = M_HSL; |
| 449 |
return *this; |
| 450 |
} |
| 451 |
|
| 452 |
Color &Color::hsl_saturation(float s) noexcept |
| 453 |
{ |
| 454 |
calc_hsl().S = clamp(s); |
| 455 |
mask = M_HSL; |
| 456 |
return *this; |
| 457 |
} |
| 458 |
|
| 459 |
Color &Color::hsl_lightness(float l) noexcept |
| 460 |
{ |
| 461 |
calc_hsl().L = clamp(l); |
| 462 |
mask = M_HSL; |
| 463 |
return *this; |
| 464 |
} |
| 465 |
|
| 466 |
Color &Color::alpha(float a) noexcept |
| 467 |
{ |
| 468 |
A = a; |
| 469 |
return *this; |
| 470 |
} |
| 471 |
|
| 472 |
Color &Color::opacity(float o) noexcept |
| 473 |
{ |
| 474 |
A = 1.0f - o; |
| 475 |
return *this; |
| 476 |
} |
| 477 |
|
| 478 |
Color &Color::set_rgb24(uint32_t v) noexcept |
| 479 |
{ |
| 480 |
rgb.R = ((v >> 16) & 0xff) * FLOAT_RCOL_COEFF; |
| 481 |
rgb.G = ((v >> 8) & 0xff) * FLOAT_RCOL_COEFF; |
| 482 |
rgb.B = (v & 0xff) * FLOAT_RCOL_COEFF; |
| 483 |
A = 0.0f; |
| 484 |
mask = M_RGB; |
| 485 |
|
| 486 |
return *this; |
| 487 |
} |
| 488 |
|
| 489 |
Color &Color::set_rgba32(uint32_t v) noexcept |
| 490 |
{ |
| 491 |
rgb.R = ((v >> 16) & 0xff) * FLOAT_RCOL_COEFF; |
| 492 |
rgb.G = ((v >> 8) & 0xff) * FLOAT_RCOL_COEFF; |
| 493 |
rgb.B = (v & 0xff) * FLOAT_RCOL_COEFF; |
| 494 |
A = ((v >> 24) & 0xff) * FLOAT_RCOL_COEFF; |
| 495 |
mask = M_RGB; |
| 496 |
|
| 497 |
return *this; |
| 498 |
} |
| 499 |
|
| 500 |
Color &Color::set_dev_rgba32(uint32_t v) noexcept |
| 501 |
{ |
| 502 |
rgb.R = ((v >> 16) & 0xff) * FLOAT_RCOL_COEFF; |
| 503 |
rgb.G = ((v >> 8) & 0xff) * FLOAT_RCOL_COEFF; |
| 504 |
rgb.B = (v & 0xff) * FLOAT_RCOL_COEFF; |
| 505 |
A = 1.0f - ((v >> 24) & 0xff) * FLOAT_RCOL_COEFF; |
| 506 |
mask = M_RGB; |
| 507 |
|
| 508 |
return *this; |
| 509 |
} |
| 510 |
|
| 511 |
Color &Color::set_prgba32(uint32_t v) noexcept |
| 512 |
{ |
| 513 |
const uint32_t int_a = ((v >> 24) & 0xff); |
| 514 |
if (int_a > 0) |
| 515 |
{ |
| 516 |
const float a = int_a * FLOAT_RCOL_COEFF; |
| 517 |
const float ra = FLOAT_RCOL_COEFF / a; |
| 518 |
|
| 519 |
rgb.R = ((v >> 16) & 0xff) * ra; |
| 520 |
rgb.G = ((v >> 8) & 0xff) * ra; |
| 521 |
rgb.B = (v & 0xff) * ra; |
| 522 |
A = 1.0f - a; |
| 523 |
} |
| 524 |
else |
| 525 |
{ |
| 526 |
rgb.R = 0.0f; |
| 527 |
rgb.G = 0.0f; |
| 528 |
rgb.B = 0.0f; |
| 529 |
A = 1.0f; |
| 530 |
} |
| 531 |
mask = M_RGB; |
| 532 |
|
| 533 |
return *this; |
| 534 |
} |
| 535 |
|
| 536 |
Color &Color::set_hsl24(uint32_t v) noexcept |
| 537 |
{ |
| 538 |
hsl.H = ((v >> 16) & 0xff) * FLOAT_RCOL_COEFF; |
| 539 |
hsl.S = ((v >> 8) & 0xff) * FLOAT_RCOL_COEFF; |
| 540 |
hsl.L = (v & 0xff) * FLOAT_RCOL_COEFF; |
| 541 |
A = 0.0f; |
| 542 |
mask = M_HSL; |
| 543 |
|
| 544 |
return *this; |
| 545 |
} |
| 546 |
|
| 547 |
Color &Color::set_hsla32(uint32_t v) noexcept |
| 548 |
{ |
| 549 |
hsl.H = ((v >> 16) & 0xff) * FLOAT_RCOL_COEFF; |
| 550 |
hsl.S = ((v >> 8) & 0xff) * FLOAT_RCOL_COEFF; |
| 551 |
hsl.L = (v & 0xff) * FLOAT_RCOL_COEFF; |
| 552 |
A = ((v >> 24) & 0xff) * FLOAT_RCOL_COEFF; |
| 553 |
mask = M_HSL; |
| 554 |
|
| 555 |
return *this; |
| 556 |
} |
| 557 |
|
| 558 |
bool Color::hsl_to_rgb() const noexcept |
| 559 |
{ |
| 560 |
if (!(mask & M_HSL)) |
| 561 |
return false; |
| 562 |
|
| 563 |
// Convert HSL to RGB |
| 564 |
if (hsl.S > 0.0f) |
| 565 |
{ |
| 566 |
float Q = (hsl.L < 0.5f) ? hsl.L * (1.0f + hsl.S) : hsl.L + hsl.S - hsl.L*hsl.S; |
| 567 |
float P = hsl.L + hsl.L - Q; // 2.0 * L - Q |
| 568 |
float D = 6.0f * (Q - P); |
| 569 |
|
| 570 |
float TR = hsl.H + HSL_RGB_1_3; |
| 571 |
float TG = hsl.H; |
| 572 |
float TB = hsl.H - HSL_RGB_1_3; |
| 573 |
|
| 574 |
if (TR > 1.0f) |
| 575 |
TR -= 1.0f; |
| 576 |
if (TB < 0.0f) |
| 577 |
TB += 1.0f; |
| 578 |
|
| 579 |
if (TR < 0.5f) |
| 580 |
rgb.R = (TR < HSL_RGB_1_6) ? P + D * TR : Q; |
| 581 |
else |
| 582 |
rgb.R = (TR < HSL_RGB_2_3) ? P + D * (HSL_RGB_2_3 - TR) : P; |
| 583 |
|
| 584 |
if (TG < 0.5f) |
| 585 |
rgb.G = (TG < HSL_RGB_1_6) ? P + D * TG : Q; |
| 586 |
else |
| 587 |
rgb.G = (TG < HSL_RGB_2_3) ? P + D * (HSL_RGB_2_3 - TG) : P; |
| 588 |
|
| 589 |
if (TB < 0.5f) |
| 590 |
rgb.B = (TB < HSL_RGB_1_6) ? P + D * TB : Q; |
| 591 |
else |
| 592 |
rgb.B = (TB < HSL_RGB_2_3) ? P + D * (HSL_RGB_2_3 - TB) : P; |
| 593 |
} |
| 594 |
else |
| 595 |
{ |
| 596 |
rgb.R = hsl.L; |
| 597 |
rgb.G = hsl.L; |
| 598 |
rgb.B = hsl.L; |
| 599 |
} |
| 600 |
|
| 601 |
mask |= M_RGB; |
| 602 |
|
| 603 |
return true; |
| 604 |
} |
| 605 |
|
| 606 |
bool Color::xyz_to_rgb() const noexcept |
| 607 |
{ |
| 608 |
if (!(mask & M_XYZ)) |
| 609 |
return false; |
| 610 |
|
| 611 |
// Convert XYZ to RGB |
| 612 |
float r = xyz.X * 0.032406f + xyz.Y * -0.015372f + xyz.Z * -0.004986f; |
| 613 |
float g = xyz.X * -0.009689f + xyz.Y * 0.018758f + xyz.Z * 0.000415f; |
| 614 |
float b = xyz.X * 0.000557f + xyz.Y * -0.002040f + xyz.Z * 0.010570f; |
| 615 |
|
| 616 |
r = (r > 0.0031308f) ? 1.055f * (powf(r, ONE_DIV_2p4)) - 0.055f : ONE_DIV_12p92 * r; |
| 617 |
g = (g > 0.0031308f) ? 1.055f * (powf(g, ONE_DIV_2p4)) - 0.055f : ONE_DIV_12p92 * g; |
| 618 |
b = (b > 0.0031308f) ? 1.055f * (powf(b, ONE_DIV_2p4)) - 0.055f : ONE_DIV_12p92 * b; |
| 619 |
|
| 620 |
rgb.R = clamp(r); |
| 621 |
rgb.G = clamp(g); |
| 622 |
rgb.B = clamp(b); |
| 623 |
|
| 624 |
mask |= M_RGB; |
| 625 |
|
| 626 |
return true; |
| 627 |
} |
| 628 |
|
| 629 |
bool Color::lab_to_xyz() const noexcept |
| 630 |
{ |
| 631 |
if (!(mask & M_LAB)) |
| 632 |
return false; |
| 633 |
|
| 634 |
float y = (lab.L * ONE_DIV_116) + DIV_16_BY_116; |
| 635 |
float x = (lab.A * 0.002f) + y; |
| 636 |
float z = (lab.B * -0.005f) + y; |
| 637 |
|
| 638 |
float y3 = y*y*y; |
| 639 |
float x3 = x*x*x; |
| 640 |
float z3 = z*z*z; |
| 641 |
|
| 642 |
y = (y3 > 0.008856f) ? y3 : (y - DIV_16_BY_116) * DIV_ONE_BY_7p787; |
| 643 |
x = (x3 > 0.008856f) ? x3 : (x - DIV_16_BY_116) * DIV_ONE_BY_7p787; |
| 644 |
z = (z3 > 0.008856f) ? z3 : (z - DIV_16_BY_116) * DIV_ONE_BY_7p787; |
| 645 |
|
| 646 |
xyz.X = x * 95.047f; |
| 647 |
xyz.Y = y * 100.0f; |
| 648 |
xyz.Z = z * 108.883f; |
| 649 |
|
| 650 |
mask |= M_XYZ; |
| 651 |
|
| 652 |
return true; |
| 653 |
} |
| 654 |
|
| 655 |
bool Color::lch_to_lab() const noexcept |
| 656 |
{ |
| 657 |
if (!(mask & M_LCH)) |
| 658 |
return false; |
| 659 |
|
| 660 |
lab.L = lch.L; |
| 661 |
lab.A = cosf(lch.H * DEG_TO_RAD) * lch.C; |
| 662 |
lab.B = sinf(lch.H * DEG_TO_RAD) * lch.C; |
| 663 |
|
| 664 |
mask |= M_LAB; |
| 665 |
|
| 666 |
return true; |
| 667 |
} |
| 668 |
|
| 669 |
bool Color::cmyk_to_rgb() const noexcept |
| 670 |
{ |
| 671 |
if (!(mask & M_CMYK)) |
| 672 |
return false; |
| 673 |
|
| 674 |
float k1 = 1.0f - cmyk.K; |
| 675 |
rgb.R = k1 - cmyk.C * k1; |
| 676 |
rgb.G = k1 - cmyk.M * k1; |
| 677 |
rgb.B = k1 - cmyk.Y * k1; |
| 678 |
|
| 679 |
mask |= M_CMYK; |
| 680 |
|
| 681 |
return true; |
| 682 |
} |
| 683 |
|
| 684 |
Color::rgb_t &Color::calc_rgb() const noexcept |
| 685 |
{ |
| 686 |
// Check if RGB is present |
| 687 |
if (mask & M_RGB) |
| 688 |
return rgb; |
| 689 |
|
| 690 |
// Try to convert HSL -> RGB |
| 691 |
if (hsl_to_rgb()) |
| 692 |
return rgb; |
| 693 |
|
| 694 |
// Try to convert XYZ -> RGB |
| 695 |
if (xyz_to_rgb()) |
| 696 |
return rgb; |
| 697 |
|
| 698 |
// Try to convert LAB -> XYZ -> RGB |
| 699 |
if (lab_to_xyz()) |
| 700 |
{ |
| 701 |
if (xyz_to_rgb()) |
| 702 |
return rgb; |
| 703 |
} |
| 704 |
|
| 705 |
// Try to convert LCH -> LAB -> XYZ -> RGB |
| 706 |
if (lch_to_lab()) |
| 707 |
{ |
| 708 |
if (lab_to_xyz()) |
| 709 |
{ |
| 710 |
if (xyz_to_rgb()) |
| 711 |
return rgb; |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
// Try to convert CMYK -> RGB |
| 716 |
if (cmyk_to_rgb()) |
| 717 |
return rgb; |
| 718 |
|
| 719 |
// Fallback case |
| 720 |
mask |= M_RGB; |
| 721 |
|
| 722 |
return rgb; |
| 723 |
} |
| 724 |
|
| 725 |
Color::hsl_t &Color::calc_hsl() const noexcept |
| 726 |
{ |
| 727 |
if (mask & M_HSL) |
| 728 |
return hsl; |
| 729 |
|
| 730 |
// At this moment we can convert color to HSL only from RGB |
| 731 |
calc_rgb(); |
| 732 |
|
| 733 |
float cmax = lsp_max(rgb.R, rgb.G, rgb.B); |
| 734 |
float cmin = lsp_min(rgb.R, rgb.G, rgb.B); |
| 735 |
float d = cmax - cmin; |
| 736 |
|
| 737 |
hsl.H = 0.0f; |
| 738 |
hsl.S = 0.0f; |
| 739 |
hsl.L = 0.5f * (cmax + cmin); |
| 740 |
|
| 741 |
// Calculate hue |
| 742 |
if (rgb.R == cmax) |
| 743 |
{ |
| 744 |
hsl.H = (rgb.G - rgb.B) / d; |
| 745 |
if (rgb.G < rgb.B) |
| 746 |
hsl.H += 6.0f; |
| 747 |
} |
| 748 |
else if (rgb.G == cmax) |
| 749 |
hsl.H = (rgb.B - rgb.R) / d + 2.0f; |
| 750 |
else if (rgb.B == cmax) |
| 751 |
hsl.H = (rgb.R - rgb.G) / d + 4.0f; |
| 752 |
|
| 753 |
// Calculate saturation |
| 754 |
if (hsl.L <= 0.5f) |
| 755 |
hsl.S = (hsl.L <= 0.0f) ? 0.0f : 0.5f * d / hsl.L; |
| 756 |
else if (hsl.L > 0.5f) |
| 757 |
hsl.S = (hsl.L < 1.0f) ? 0.5f * d / (1.0f - hsl.L) : 0.0f; |
| 758 |
|
| 759 |
// Normalize hue |
| 760 |
hsl.H *= HSL_RGB_1_6; |
| 761 |
|
| 762 |
mask |= M_HSL; |
| 763 |
|
| 764 |
return hsl; |
| 765 |
} |
| 766 |
|
| 767 |
Color::xyz_t &Color::calc_xyz() const noexcept |
| 768 |
{ |
| 769 |
if (mask & M_XYZ) |
| 770 |
return xyz; |
| 771 |
|
| 772 |
// At this moment we can convert color to XYZ only from RGB |
| 773 |
calc_rgb(); |
| 774 |
|
| 775 |
float r = (rgb.R > 0.04045f) ? powf((rgb.R + 0.055f) * ONE_DIV_1p055, 2.4f) : rgb.R * ONE_DIV_12p92; |
| 776 |
float g = (rgb.G > 0.04045f) ? powf((rgb.G + 0.055f) * ONE_DIV_1p055, 2.4f) : rgb.G * ONE_DIV_12p92; |
| 777 |
float b = (rgb.B > 0.04045f) ? powf((rgb.B + 0.055f) * ONE_DIV_1p055, 2.4f) : rgb.B * ONE_DIV_12p92; |
| 778 |
|
| 779 |
xyz.X = r * 41.24f + g * 35.76f + b * 18.05f; |
| 780 |
xyz.Y = r * 21.26f + g * 71.52f + b * 7.22f; |
| 781 |
xyz.Z = r * 1.93f + g * 11.92f + b * 95.05f; |
| 782 |
|
| 783 |
mask |= M_XYZ; |
| 784 |
|
| 785 |
return xyz; |
| 786 |
} |
| 787 |
|
| 788 |
Color::lab_t &Color::calc_lab() const noexcept |
| 789 |
{ |
| 790 |
if (mask & M_LAB) |
| 791 |
return lab; |
| 792 |
|
| 793 |
// At this moment we can convert color to LAB only from XYZ |
| 794 |
calc_xyz(); |
| 795 |
|
| 796 |
float x = xyz.X * ONE_DIV_95p047; |
| 797 |
float y = xyz.Y * 0.01f;; |
| 798 |
float z = xyz.Z * ONE_DIV_108p883; |
| 799 |
|
| 800 |
x = (x > 0.008856f) ? powf(x, ONE_DIV_3) : (7.787f * x) + DIV_16_BY_116; |
| 801 |
y = (y > 0.008856f) ? powf(y, ONE_DIV_3) : (7.787f * y) + DIV_16_BY_116; |
| 802 |
z = (z > 0.008856f) ? powf(z, ONE_DIV_3) : (7.787f * z) + DIV_16_BY_116; |
| 803 |
|
| 804 |
lab.L = (116.0f * y) - 16.0f; |
| 805 |
lab.A = 500.0f * (x - y); |
| 806 |
lab.B = 200.0f * (y - z); |
| 807 |
|
| 808 |
mask |= M_LAB; |
| 809 |
|
| 810 |
return lab; |
| 811 |
} |
| 812 |
|
| 813 |
Color::lch_t &Color::calc_lch() const noexcept |
| 814 |
{ |
| 815 |
if (mask & M_LCH) |
| 816 |
return lch; |
| 817 |
|
| 818 |
// At this moment we can convert color to LCH only from LAB |
| 819 |
calc_lab(); |
| 820 |
|
| 821 |
float h = atan2f(lab.B, lab.A) * RAD_TO_DEG; |
| 822 |
if (h < 0) |
| 823 |
h += 360.0f; |
| 824 |
|
| 825 |
lch.L = lab.L; |
| 826 |
lch.C = sqrtf(lab.A * lab.A + lab.B * lab.B); |
| 827 |
lch.H = h; |
| 828 |
|
| 829 |
mask |= M_LCH; |
| 830 |
|
| 831 |
return lch; |
| 832 |
} |
| 833 |
|
| 834 |
Color::cmyk_t &Color::calc_cmyk() const noexcept |
| 835 |
{ |
| 836 |
if (mask & M_CMYK) |
| 837 |
return cmyk; |
| 838 |
|
| 839 |
float C = 1.0f - rgb.R; |
| 840 |
float M = 1.0f - rgb.G; |
| 841 |
float Y = 1.0f - rgb.B; |
| 842 |
|
| 843 |
cmyk.K = lsp_min(C, M, Y); |
| 844 |
if (cmyk.K >= 1.0f) |
| 845 |
{ |
| 846 |
cmyk.C = 0.0f; |
| 847 |
cmyk.M = 0.0f; |
| 848 |
cmyk.Y = 0.0f; |
| 849 |
} |
| 850 |
else |
| 851 |
{ |
| 852 |
float rk = 1.0f / (1.0f - cmyk.K); |
| 853 |
|
| 854 |
cmyk.C = (C - cmyk.K) * rk; |
| 855 |
cmyk.M = (M - cmyk.K) * rk; |
| 856 |
cmyk.Y = (Y - cmyk.K) * rk; |
| 857 |
} |
| 858 |
|
| 859 |
mask |= M_CMYK; |
| 860 |
|
| 861 |
return cmyk; |
| 862 |
} |
| 863 |
|
| 864 |
Color &Color::set_rgb(float r, float g, float b) noexcept |
| 865 |
{ |
| 866 |
mask = M_RGB; |
| 867 |
rgb.R = clamp(r); |
| 868 |
rgb.G = clamp(g); |
| 869 |
rgb.B = clamp(b); |
| 870 |
|
| 871 |
return *this; |
| 872 |
} |
| 873 |
|
| 874 |
Color &Color::set_rgba(float r, float g, float b, float a) noexcept |
| 875 |
{ |
| 876 |
mask = M_RGB; |
| 877 |
rgb.R = clamp(r); |
| 878 |
rgb.G = clamp(g); |
| 879 |
rgb.B = clamp(b); |
| 880 |
A = clamp(a); |
| 881 |
|
| 882 |
return *this; |
| 883 |
} |
| 884 |
|
| 885 |
Color &Color::set_hsl(float h, float s, float l) noexcept |
| 886 |
{ |
| 887 |
mask = M_HSL; |
| 888 |
hsl.H = clamp(h); |
| 889 |
hsl.S = clamp(s); |
| 890 |
hsl.L = clamp(l); |
| 891 |
|
| 892 |
return *this; |
| 893 |
} |
| 894 |
|
| 895 |
Color &Color::set_hsla(float h, float s, float l, float a) noexcept |
| 896 |
{ |
| 897 |
mask = M_HSL; |
| 898 |
hsl.H = clamp(h); |
| 899 |
hsl.S = clamp(s); |
| 900 |
hsl.L = clamp(l); |
| 901 |
A = clamp(a); |
| 902 |
|
| 903 |
return *this; |
| 904 |
} |
| 905 |
|
| 906 |
const Color &Color::get_rgb(float &r, float &g, float &b) const noexcept |
| 907 |
{ |
| 908 |
calc_rgb(); |
| 909 |
r = rgb.R; |
| 910 |
g = rgb.G; |
| 911 |
b = rgb.B; |
| 912 |
|
| 913 |
return *this; |
| 914 |
} |
| 915 |
|
| 916 |
const Color &Color::get_rgba(float &r, float &g, float &b, float &a) const noexcept |
| 917 |
{ |
| 918 |
calc_rgb(); |
| 919 |
r = rgb.R; |
| 920 |
g = rgb.G; |
| 921 |
b = rgb.B; |
| 922 |
a = A; |
| 923 |
|
| 924 |
return *this; |
| 925 |
} |
| 926 |
|
| 927 |
const Color &Color::get_rgbo(float &r, float &g, float &b, float &o) const noexcept |
| 928 |
{ |
| 929 |
calc_rgb(); |
| 930 |
r = rgb.R; |
| 931 |
g = rgb.G; |
| 932 |
b = rgb.B; |
| 933 |
o = 1.0f - A; |
| 934 |
|
| 935 |
return *this; |
| 936 |
} |
| 937 |
|
| 938 |
const Color &Color::get_hsl(float &h, float &s, float &l) const noexcept |
| 939 |
{ |
| 940 |
calc_hsl(); |
| 941 |
h = hsl.H; |
| 942 |
s = hsl.S; |
| 943 |
l = hsl.L; |
| 944 |
|
| 945 |
return *this; |
| 946 |
} |
| 947 |
|
| 948 |
const Color &Color::get_hsla(float &h, float &s, float &l, float &a) const noexcept |
| 949 |
{ |
| 950 |
calc_hsl(); |
| 951 |
h = hsl.H; |
| 952 |
s = hsl.S; |
| 953 |
l = hsl.L; |
| 954 |
a = A; |
| 955 |
|
| 956 |
return *this; |
| 957 |
} |
| 958 |
|
| 959 |
Color &Color::get_rgb(float &r, float &g, float &b) noexcept |
| 960 |
{ |
| 961 |
calc_rgb(); |
| 962 |
r = rgb.R; |
| 963 |
g = rgb.G; |
| 964 |
b = rgb.B; |
| 965 |
|
| 966 |
return *this; |
| 967 |
} |
| 968 |
|
| 969 |
Color &Color::get_rgba(float &r, float &g, float &b, float &a) noexcept |
| 970 |
{ |
| 971 |
calc_rgb(); |
| 972 |
r = rgb.R; |
| 973 |
g = rgb.G; |
| 974 |
b = rgb.B; |
| 975 |
a = A; |
| 976 |
|
| 977 |
return *this; |
| 978 |
} |
| 979 |
|
| 980 |
Color &Color::get_hsl(float &h, float &s, float &l) noexcept |
| 981 |
{ |
| 982 |
calc_hsl(); |
| 983 |
h = hsl.H; |
| 984 |
s = hsl.S; |
| 985 |
l = hsl.L; |
| 986 |
|
| 987 |
return *this; |
| 988 |
} |
| 989 |
|
| 990 |
Color &Color::get_hsla(float &h, float &s, float &l, float &a) noexcept |
| 991 |
{ |
| 992 |
calc_hsl(); |
| 993 |
h = hsl.H; |
| 994 |
s = hsl.S; |
| 995 |
l = hsl.L; |
| 996 |
a = A; |
| 997 |
|
| 998 |
return *this; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
Color &Color::blend(const Color &c, float alpha) noexcept |
| 1003 |
{ |
| 1004 |
float r1, g1, b1, r2, g2, b2; |
| 1005 |
get_rgb(r1, g1, b1); |
| 1006 |
c.get_rgb(r2, g2, b2); |
| 1007 |
set_rgb(r2 + (r1 - r2) * alpha, g2 + (g1 - g2) * alpha, b2 + (b1 - b2) * alpha); |
| 1008 |
|
| 1009 |
return *this; |
| 1010 |
} |
| 1011 |
|
| 1012 |
Color &Color::blend(float r, float g, float b, float alpha) noexcept |
| 1013 |
{ |
| 1014 |
float r1, g1, b1; |
| 1015 |
get_rgb(r1, g1, b1); |
| 1016 |
set_rgb(r + (r1 - r) * alpha, g + (g1 - g) * alpha, b + (b1 - b) * alpha); |
| 1017 |
|
| 1018 |
return *this; |
| 1019 |
} |
| 1020 |
|
| 1021 |
Color &Color::blend(const Color &c1, const Color &c2, float alpha) noexcept |
| 1022 |
{ |
| 1023 |
float r1, g1, b1, r2, g2, b2; |
| 1024 |
c1.get_rgb(r1, g1, b1); |
| 1025 |
c2.get_rgb(r2, g2, b2); |
| 1026 |
set_rgb(r2 + (r1 - r2) * alpha, g2 + (g1 - g2) * alpha, b2 + (b1 - b2) * alpha); |
| 1027 |
|
| 1028 |
return *this; |
| 1029 |
} |
| 1030 |
|
| 1031 |
Color &Color::darken(float amount) noexcept |
| 1032 |
{ |
| 1033 |
float r, g, b; |
| 1034 |
get_rgb(r, g, b); |
| 1035 |
|
| 1036 |
float a = 1.0 - amount; |
| 1037 |
set_rgb(r * a, g * a, b * a); |
| 1038 |
|
| 1039 |
return *this; |
| 1040 |
} |
| 1041 |
|
| 1042 |
Color &Color::lighten(float amount) noexcept |
| 1043 |
{ |
| 1044 |
float r, g, b; |
| 1045 |
get_rgb(r, g, b); |
| 1046 |
|
| 1047 |
float a = 1.0 - amount; |
| 1048 |
set_rgb(r + (1.0 - r) * a, g + (1.0 - g) * a, b + (1.0 - b) * a); |
| 1049 |
|
| 1050 |
return *this; |
| 1051 |
} |
| 1052 |
|
| 1053 |
void Color::scale_hsl_lightness(float amount) noexcept |
| 1054 |
{ |
| 1055 |
calc_hsl(); |
| 1056 |
hsl.L = clamp(amount * hsl.L); |
| 1057 |
mask = M_HSL; |
| 1058 |
} |
| 1059 |
|
| 1060 |
void Color::scale_lch_luminance(float amount) noexcept |
| 1061 |
{ |
| 1062 |
calc_lch(); |
| 1063 |
lch.L = lsp_limit(amount * lch.L, 0.0f, 100.0f); |
| 1064 |
mask = M_LCH; |
| 1065 |
} |
| 1066 |
|
| 1067 |
ssize_t Color::format(char *dst, size_t len, size_t tolerance, const float *v, char prefix, bool alpha) noexcept |
| 1068 |
{ |
| 1069 |
if ((dst == NULL) || (tolerance <= 0) || (tolerance > 4)) |
| 1070 |
return -STATUS_BAD_ARGUMENTS; |
| 1071 |
size_t required = (tolerance * (alpha ? 4 : 3)) + 2; // Number of hex characters x number of colors + 2 symbols |
| 1072 |
if (len < required) |
| 1073 |
return -STATUS_OVERFLOW; |
| 1074 |
|
| 1075 |
// Calculate maximum value |
| 1076 |
const char *fmt; |
| 1077 |
int tol; |
| 1078 |
|
| 1079 |
if (alpha) |
| 1080 |
{ |
| 1081 |
switch (tolerance) |
| 1082 |
{ |
| 1083 |
case 1: |
| 1084 |
fmt = "%c%01x%01x%01x%01x"; |
| 1085 |
tol = 0xf; |
| 1086 |
break; |
| 1087 |
case 3: |
| 1088 |
fmt = "%c%03x%03x%03x%03x"; |
| 1089 |
tol = 0xfff; |
| 1090 |
break; |
| 1091 |
case 4: |
| 1092 |
fmt = "%c%04x%04x%04x%04x"; |
| 1093 |
tol = 0xffff; |
| 1094 |
break; |
| 1095 |
default: |
| 1096 |
fmt = "%c%02x%02x%02x%02x"; |
| 1097 |
tol = 0xff; |
| 1098 |
break; |
| 1099 |
} |
| 1100 |
|
| 1101 |
return ::snprintf( |
| 1102 |
dst, len, fmt, prefix, |
| 1103 |
int(v[3] * tol + 0.25f) & tol, |
| 1104 |
int(v[0] * tol + 0.25f) & tol, |
| 1105 |
int(v[1] * tol + 0.25f) & tol, |
| 1106 |
int(v[2] * tol + 0.25f) & tol |
| 1107 |
); |
| 1108 |
} |
| 1109 |
else |
| 1110 |
{ |
| 1111 |
switch (tolerance) |
| 1112 |
{ |
| 1113 |
case 1: |
| 1114 |
fmt = "%c%01x%01x%01x"; |
| 1115 |
tol = 0xf; |
| 1116 |
break; |
| 1117 |
case 3: |
| 1118 |
fmt = "%c%03x%03x%03x"; |
| 1119 |
tol = 0xfff; |
| 1120 |
break; |
| 1121 |
case 4: |
| 1122 |
fmt = "%c%04x%04x%04x"; |
| 1123 |
tol = 0xffff; |
| 1124 |
break; |
| 1125 |
default: |
| 1126 |
fmt = "%c%02x%02x%02x"; |
| 1127 |
tol = 0xff; |
| 1128 |
break; |
| 1129 |
} |
| 1130 |
|
| 1131 |
return ::snprintf( |
| 1132 |
dst, len, fmt, prefix, |
| 1133 |
int(v[0] * tol + 0.25f) & tol, |
| 1134 |
int(v[1] * tol + 0.25f) & tol, |
| 1135 |
int(v[2] * tol + 0.25f) & tol |
| 1136 |
); |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
ssize_t Color::format_rgb(char *dst, size_t len, size_t tolerance) const noexcept |
| 1141 |
{ |
| 1142 |
float v[3]; |
| 1143 |
get_rgb(v[0], v[1], v[2]); |
| 1144 |
return format(dst, len, tolerance, v, '#', false); |
| 1145 |
} |
| 1146 |
|
| 1147 |
ssize_t Color::format_hsl(char *dst, size_t len, size_t tolerance) const noexcept |
| 1148 |
{ |
| 1149 |
float v[3]; |
| 1150 |
get_hsl(v[0], v[1], v[2]); |
| 1151 |
return format(dst, len, tolerance, v, '@', false); |
| 1152 |
} |
| 1153 |
|
| 1154 |
ssize_t Color::format_rgba(char *dst, size_t len, size_t tolerance) const noexcept |
| 1155 |
{ |
| 1156 |
float v[4]; |
| 1157 |
get_rgba(v[0], v[1], v[2], v[3]); |
| 1158 |
return format(dst, len, tolerance, v, '#', true); |
| 1159 |
} |
| 1160 |
|
| 1161 |
ssize_t Color::format_hsla(char *dst, size_t len, size_t tolerance) const noexcept |
| 1162 |
{ |
| 1163 |
float v[4]; |
| 1164 |
get_hsla(v[0], v[1], v[2], v[3]); |
| 1165 |
return format(dst, len, tolerance, v, '@', true); |
| 1166 |
} |
| 1167 |
|
| 1168 |
ssize_t Color::format_rgb(LSPString *dst, size_t tolerance) const noexcept |
| 1169 |
{ |
| 1170 |
char tmp[32]; |
| 1171 |
ssize_t res = format_rgb(tmp, sizeof(tmp), tolerance); |
| 1172 |
if (res < 0) |
| 1173 |
return res; |
| 1174 |
return (dst->set_ascii(tmp)) ? res : -STATUS_NO_MEM; |
| 1175 |
} |
| 1176 |
|
| 1177 |
ssize_t Color::format_rgba(LSPString *dst, size_t tolerance) const noexcept |
| 1178 |
{ |
| 1179 |
char tmp[32]; |
| 1180 |
ssize_t res = format_rgba(tmp, sizeof(tmp), tolerance); |
| 1181 |
if (res < 0) |
| 1182 |
return res; |
| 1183 |
return (dst->set_ascii(tmp)) ? res : -STATUS_NO_MEM; |
| 1184 |
} |
| 1185 |
|
| 1186 |
ssize_t Color::format_hsl(LSPString *dst, size_t tolerance) const noexcept |
| 1187 |
{ |
| 1188 |
char tmp[32]; |
| 1189 |
ssize_t res = format_hsl(tmp, sizeof(tmp), tolerance); |
| 1190 |
if (res < 0) |
| 1191 |
return res; |
| 1192 |
return (dst->set_ascii(tmp)) ? res : -STATUS_NO_MEM; |
| 1193 |
} |
| 1194 |
|
| 1195 |
ssize_t Color::format_hsla(LSPString *dst, size_t tolerance) const noexcept |
| 1196 |
{ |
| 1197 |
char tmp[32]; |
| 1198 |
ssize_t res = format_hsla(tmp, sizeof(tmp), tolerance); |
| 1199 |
if (res < 0) |
| 1200 |
return res; |
| 1201 |
return (dst->set_ascii(tmp)) ? res : -STATUS_NO_MEM; |
| 1202 |
} |
| 1203 |
|
| 1204 |
status_t Color::parse_hex(float *dst, size_t n, char prefix, const char *src, size_t len) noexcept |
| 1205 |
{ |
| 1206 |
if (src == NULL) |
| 1207 |
return STATUS_BAD_ARGUMENTS; |
| 1208 |
|
| 1209 |
const char *end = &src[len]; |
| 1210 |
|
| 1211 |
src = skip_whitespace(src, end); |
| 1212 |
if (src == end) |
| 1213 |
return STATUS_NO_DATA; |
| 1214 |
if (*(src++) != prefix) |
| 1215 |
return STATUS_BAD_FORMAT; |
| 1216 |
if (src >= end) |
| 1217 |
return STATUS_BAD_FORMAT; |
| 1218 |
|
| 1219 |
const char *hex = skip_hex(src, end); |
| 1220 |
const char *tail = skip_whitespace(hex, end); |
| 1221 |
if (tail != end) |
| 1222 |
return STATUS_BAD_FORMAT; |
| 1223 |
size_t hex_len = hex - src; |
| 1224 |
|
| 1225 |
// Determine the length of each component |
| 1226 |
if (hex_len % n) |
| 1227 |
return STATUS_BAD_FORMAT; |
| 1228 |
hex_len /= n; |
| 1229 |
if ((hex_len <= 0) || (hex_len > 4)) |
| 1230 |
return STATUS_BAD_FORMAT; |
| 1231 |
float norm = 1.0f / ((0x1 << (hex_len << 2)) - 1); |
| 1232 |
|
| 1233 |
// Read components |
| 1234 |
while (n--) |
| 1235 |
{ |
| 1236 |
size_t v = 0; |
| 1237 |
for (size_t i=0; i<hex_len; ++i, ++src) |
| 1238 |
v = (v << 4) | hex_char(*src); |
| 1239 |
|
| 1240 |
*(dst++) = v * norm; |
| 1241 |
} |
| 1242 |
|
| 1243 |
return STATUS_OK; |
| 1244 |
} |
| 1245 |
|
| 1246 |
status_t Color::parse_numeric(float *dst, size_t nmin, size_t nmax, const char *prefix, const char *src, size_t len) noexcept |
| 1247 |
{ |
| 1248 |
// Save and update locale |
| 1249 |
SET_LOCALE_SCOPED(LC_NUMERIC, "C"); |
| 1250 |
|
| 1251 |
status_t res = parse_cnumeric(dst, nmin, nmax, prefix, src, len); |
| 1252 |
|
| 1253 |
return res; |
| 1254 |
} |
| 1255 |
|
| 1256 |
status_t Color::parse_cnumeric(float *dst, size_t nmin, size_t nmax, const char *prefix, const char *src, size_t len) noexcept |
| 1257 |
{ |
| 1258 |
const char *end = &src[len]; |
| 1259 |
|
| 1260 |
// Parse beginning of statement |
| 1261 |
if ((src = skip_whitespace(src, end)) == end) // whitespace |
| 1262 |
return STATUS_NO_DATA; |
| 1263 |
if ((src = match_prefix(src, end, prefix)) == end) // prefix |
| 1264 |
return STATUS_BAD_FORMAT; |
| 1265 |
if ((src = skip_whitespace(src, end)) == end) // whitespace |
| 1266 |
return STATUS_BAD_FORMAT; |
| 1267 |
if ((src = match_char(src, end, '(')) == end) // ( |
| 1268 |
return STATUS_BAD_FORMAT; |
| 1269 |
|
| 1270 |
// Main loop: parse digits |
| 1271 |
size_t count = 0; |
| 1272 |
while (src < end) |
| 1273 |
{ |
| 1274 |
// Skip whitespace |
| 1275 |
if ((src = skip_whitespace(src, end)) == end) |
| 1276 |
return STATUS_BAD_FORMAT; |
| 1277 |
|
| 1278 |
// Find next delimiting character |
| 1279 |
const char *next = match_chars(src, end, ",) \t\r\n"); |
| 1280 |
if (next == end) |
| 1281 |
return STATUS_BAD_FORMAT; |
| 1282 |
|
| 1283 |
// Overflow of number of max possible values? |
| 1284 |
if (count >= nmax) |
| 1285 |
return STATUS_BAD_FORMAT; |
| 1286 |
|
| 1287 |
// Parse floating-point value |
| 1288 |
char *eptr = NULL; |
| 1289 |
errno = 0; |
| 1290 |
dst[count++] = strtof(src, &eptr); |
| 1291 |
if ((errno != 0) || (eptr != next) || (eptr == src)) |
| 1292 |
return STATUS_BAD_FORMAT; |
| 1293 |
|
| 1294 |
// Move parse position |
| 1295 |
src = skip_whitespace(next, end); |
| 1296 |
if (src == end) |
| 1297 |
return STATUS_BAD_FORMAT; |
| 1298 |
char c = *(src++); |
| 1299 |
if (c == ')') |
| 1300 |
break; |
| 1301 |
else if (c != ',') |
| 1302 |
return STATUS_BAD_FORMAT; |
| 1303 |
} |
| 1304 |
|
| 1305 |
// Check that we parsed at least nmin floating-point values |
| 1306 |
if (count < nmin) |
| 1307 |
return STATUS_BAD_FORMAT; |
| 1308 |
|
| 1309 |
// Parse end of statement |
| 1310 |
if ((src = skip_whitespace(src, end)) != end) // end-of-line |
| 1311 |
return STATUS_BAD_FORMAT; |
| 1312 |
|
| 1313 |
return STATUS_OK; |
| 1314 |
} |
| 1315 |
|
| 1316 |
status_t Color::parse4(const char *src, size_t len) noexcept |
| 1317 |
{ |
| 1318 |
if (src == NULL) |
| 1319 |
return STATUS_BAD_ARGUMENTS; |
| 1320 |
|
| 1321 |
const char *end = &src[len]; |
| 1322 |
src = skip_whitespace(src, end); |
| 1323 |
if (src >= end) |
| 1324 |
return STATUS_NO_DATA; |
| 1325 |
|
| 1326 |
return (*src == '@') ? parse_hsla(src, end - src) : parse_rgba(src, end - src); |
| 1327 |
} |
| 1328 |
|
| 1329 |
status_t Color::parse3(const char *src, size_t len) noexcept |
| 1330 |
{ |
| 1331 |
if (src == NULL) |
| 1332 |
return STATUS_BAD_ARGUMENTS; |
| 1333 |
|
| 1334 |
const char *end = &src[len]; |
| 1335 |
src = skip_whitespace(src, end); |
| 1336 |
if (src >= end) |
| 1337 |
return STATUS_NO_DATA; |
| 1338 |
|
| 1339 |
return (*src == '@') ? parse_hsl(src, end - src) : parse_rgb(src, end - src); |
| 1340 |
} |
| 1341 |
|
| 1342 |
status_t Color::parse_rgba(const char *src, size_t len) noexcept |
| 1343 |
{ |
| 1344 |
float v[4]; |
| 1345 |
status_t res = parse_hex(v, 4, '#', src, len); |
| 1346 |
if (res == STATUS_OK) |
| 1347 |
set_rgba(v[1], v[2], v[3], v[0]); |
| 1348 |
return res; |
| 1349 |
} |
| 1350 |
|
| 1351 |
status_t Color::parse_hsla(const char *src, size_t len) noexcept |
| 1352 |
{ |
| 1353 |
float v[4]; |
| 1354 |
status_t res = parse_hex(v, 4, '@', src, len); |
| 1355 |
if (res == STATUS_OK) |
| 1356 |
set_hsla(v[1], v[2], v[3], v[0]); |
| 1357 |
return res; |
| 1358 |
} |
| 1359 |
|
| 1360 |
status_t Color::parse_rgb(const char *src, size_t len) noexcept |
| 1361 |
{ |
| 1362 |
float v[3]; |
| 1363 |
status_t res = parse_hex(v, 3, '#', src, len); |
| 1364 |
if (res == STATUS_OK) |
| 1365 |
set_rgba(v[0], v[1], v[2], 0.0f); |
| 1366 |
return res; |
| 1367 |
} |
| 1368 |
|
| 1369 |
status_t Color::parse_hsl(const char *src, size_t len) noexcept |
| 1370 |
{ |
| 1371 |
float v[3]; |
| 1372 |
status_t res = parse_hex(v, 3, '@', src, len); |
| 1373 |
if (res == STATUS_OK) |
| 1374 |
set_hsla(v[0], v[1], v[2], 0.0f); |
| 1375 |
return res; |
| 1376 |
} |
| 1377 |
|
| 1378 |
status_t Color::parse(const char *src, size_t len) noexcept |
| 1379 |
{ |
| 1380 |
status_t res; |
| 1381 |
float v[5]; |
| 1382 |
if ((res = parse4(src, len)) == STATUS_OK) |
| 1383 |
return res; |
| 1384 |
if ((res = parse3(src, len)) == STATUS_OK) |
| 1385 |
return res; |
| 1386 |
|
| 1387 |
// Save and update locale |
| 1388 |
SET_LOCALE_SCOPED(LC_NUMERIC, "C"); |
| 1389 |
|
| 1390 |
// Try to parse different expressions |
| 1391 |
if ((res = parse_cnumeric(v, 3, 3, "rgb", src, len)) == STATUS_OK) |
| 1392 |
set_rgba(v[0], v[1], v[2], 0.0f); |
| 1393 |
else if ((res = parse_cnumeric(v, 4, 4, "rgba", src, len)) == STATUS_OK) |
| 1394 |
set_rgba(v[0], v[1], v[2], v[3]); |
| 1395 |
else if ((res = parse_cnumeric(v, 3, 3, "hsl", src, len)) == STATUS_OK) |
| 1396 |
set_hsla(v[0] * ONE_DIV_360, v[1] * 0.01f, v[2] * 0.005f, 0.0f); |
| 1397 |
else if ((res = parse_cnumeric(v, 4, 4, "hsla", src, len)) == STATUS_OK) |
| 1398 |
set_hsla(v[0] * ONE_DIV_360, v[1] * 0.01f, v[2] * 0.005f, v[3]); |
| 1399 |
else if ((res = parse_cnumeric(v, 3, 3, "xyz", src, len)) == STATUS_OK) |
| 1400 |
set_xyza(v[0], v[1], v[2], 0.0f); |
| 1401 |
else if ((res = parse_cnumeric(v, 4, 4, "xyza", src, len)) == STATUS_OK) |
| 1402 |
set_xyza(v[0], v[1], v[2], v[3]); |
| 1403 |
else if ((res = parse_cnumeric(v, 3, 3, "lab", src, len)) == STATUS_OK) |
| 1404 |
set_laba(v[0], v[1], v[2], 0.0f); |
| 1405 |
else if ((res = parse_cnumeric(v, 4, 4, "laba", src, len)) == STATUS_OK) |
| 1406 |
set_laba(v[0], v[1], v[2], v[3]); |
| 1407 |
else if ((res = parse_cnumeric(v, 3, 3, "lch", src, len)) == STATUS_OK) |
| 1408 |
set_lcha(v[0], v[1], v[2], 0.0f); |
| 1409 |
else if ((res = parse_cnumeric(v, 4, 4, "lcha", src, len)) == STATUS_OK) |
| 1410 |
set_lcha(v[0], v[1], v[2], v[3]); |
| 1411 |
else if ((res = parse_cnumeric(v, 3, 3, "hcl", src, len)) == STATUS_OK) |
| 1412 |
set_lcha(v[2], v[1], v[0], 0.0f); |
| 1413 |
else if ((res = parse_cnumeric(v, 4, 4, "hcla", src, len)) == STATUS_OK) |
| 1414 |
set_lcha(v[2], v[1], v[0], v[3]); |
| 1415 |
else if ((res = parse_cnumeric(v, 4, 4, "cmyk", src, len)) == STATUS_OK) |
| 1416 |
set_cmyk(v[0], v[1], v[2], v[3]); |
| 1417 |
else if ((res = parse_cnumeric(v, 5, 5, "cmyka", src, len)) == STATUS_OK) |
| 1418 |
set_cmyka(v[0], v[1], v[2], v[3], v[4]); |
| 1419 |
|
| 1420 |
return res; |
| 1421 |
} |
| 1422 |
|
| 1423 |
ssize_t Color::format3(char *dst, size_t len) const noexcept |
| 1424 |
{ |
| 1425 |
// Save and update locale |
| 1426 |
SET_LOCALE_SCOPED(LC_NUMERIC, "C"); |
| 1427 |
|
| 1428 |
ssize_t res = 0; |
| 1429 |
if (is_rgb()) |
| 1430 |
res = snprintf(dst, len, "rgb(%.4f, %.4f, %.4f)", rgb.R, rgb.G, rgb.B); |
| 1431 |
else if (is_hsl()) |
| 1432 |
res = snprintf(dst, len, "hsl(%.4f, %.4f, %.4f)", hsl.H * 360.0f, hsl.S * 100.0f, hsl.L * 200.0f); |
| 1433 |
else if (is_lch()) |
| 1434 |
res = snprintf(dst, len, "hcl(%.4f, %.4f, %.4f)", lch.H, lch.C, lch.L); |
| 1435 |
else if (is_lab()) |
| 1436 |
res = snprintf(dst, len, "lab(%.4f, %.4f, %.4f)", lab.L, lab.A, lab.B); |
| 1437 |
else if (is_xyz()) |
| 1438 |
res = snprintf(dst, len, "xyz(%.4f, %.4f, %.4f)", xyz.X, xyz.Y, xyz.Z); |
| 1439 |
else if (is_cmyk()) |
| 1440 |
res = snprintf(dst, len, "cmyk(%.4f, %.4f, %.4f, %.4f)", cmyk.C, cmyk.M, cmyk.Y, cmyk.K); |
| 1441 |
else |
| 1442 |
res = snprintf(dst, len, "rgb(%.4f, %.4f, %.4f)", rgb.R, rgb.G, rgb.B); |
| 1443 |
|
| 1444 |
return res; |
| 1445 |
} |
| 1446 |
|
| 1447 |
ssize_t Color::format4(char *dst, size_t len) const noexcept |
| 1448 |
{ |
| 1449 |
// Save and update locale |
| 1450 |
SET_LOCALE_SCOPED(LC_NUMERIC, "C"); |
| 1451 |
|
| 1452 |
ssize_t res = 0; |
| 1453 |
if (is_rgb()) |
| 1454 |
res = snprintf(dst, len, "rgba(%.4f, %.4f, %.4f, %.4f)", rgb.R, rgb.G, rgb.B, A); |
| 1455 |
else if (is_hsl()) |
| 1456 |
res = snprintf(dst, len, "hsla(%.4f, %.4f, %.4f, %.4f)", hsl.H * 360.0f, hsl.S * 100.0f, hsl.L * 200.0f, A); |
| 1457 |
else if (is_lch()) |
| 1458 |
res = snprintf(dst, len, "hcla(%.4f, %.4f, %.4f, %.4f)", lch.H, lch.C, lch.L, A); |
| 1459 |
else if (is_lab()) |
| 1460 |
res = snprintf(dst, len, "laba(%.4f, %.4f, %.4f, %.4f)", lab.L, lab.A, lab.B, A); |
| 1461 |
else if (is_xyz()) |
| 1462 |
res = snprintf(dst, len, "xyza(%.4f, %.4f, %.4f, %.4f)", xyz.X, xyz.Y, xyz.Z, A); |
| 1463 |
else if (is_cmyk()) |
| 1464 |
res = snprintf(dst, len, "cmyka(%.4f, %.4f, %.4f, %.4f, %.4f)", cmyk.C, cmyk.M, cmyk.Y, cmyk.K, A); |
| 1465 |
else |
| 1466 |
res = snprintf(dst, len, "rgba(%.4f, %.4f, %.4f, %.4f)", rgb.R, rgb.G, rgb.B, A); |
| 1467 |
|
| 1468 |
return res; |
| 1469 |
} |
| 1470 |
|
| 1471 |
ssize_t Color::format3(LSPString *dst) const noexcept |
| 1472 |
{ |
| 1473 |
if (dst == NULL) |
| 1474 |
return -STATUS_BAD_ARGUMENTS; |
| 1475 |
|
| 1476 |
char buf[64]; |
| 1477 |
ssize_t res = format3(buf, sizeof(buf) / sizeof(char)); |
| 1478 |
if (res < 0) |
| 1479 |
return res; |
| 1480 |
|
| 1481 |
return (dst->set_ascii(buf, res)) ? res : -STATUS_NO_MEM; |
| 1482 |
} |
| 1483 |
|
| 1484 |
ssize_t Color::format4(LSPString *dst) const noexcept |
| 1485 |
{ |
| 1486 |
if (dst == NULL) |
| 1487 |
return -STATUS_BAD_ARGUMENTS; |
| 1488 |
|
| 1489 |
char buf[64]; |
| 1490 |
ssize_t res = format3(buf, sizeof(buf) / sizeof(char)); |
| 1491 |
if (res < 0) |
| 1492 |
return res; |
| 1493 |
|
| 1494 |
return (dst->set_ascii(buf, res)) ? res : -STATUS_NO_MEM; |
| 1495 |
} |
| 1496 |
|
| 1497 |
status_t Color::parse_rgba(const char *src) noexcept |
| 1498 |
{ |
| 1499 |
return (src != NULL) ? parse_rgba(src, ::strlen(src)) : STATUS_BAD_ARGUMENTS; |
| 1500 |
} |
| 1501 |
|
| 1502 |
status_t Color::parse_hsla(const char *src) noexcept |
| 1503 |
{ |
| 1504 |
return (src != NULL) ? parse_hsla(src, ::strlen(src)) : STATUS_BAD_ARGUMENTS; |
| 1505 |
} |
| 1506 |
|
| 1507 |
status_t Color::parse_rgb(const char *src) noexcept |
| 1508 |
{ |
| 1509 |
return (src != NULL) ? parse_rgb(src, ::strlen(src)) : STATUS_BAD_ARGUMENTS; |
| 1510 |
} |
| 1511 |
|
| 1512 |
status_t Color::parse_hsl(const char *src) noexcept |
| 1513 |
{ |
| 1514 |
return (src != NULL) ? parse_hsl(src, ::strlen(src)) : STATUS_BAD_ARGUMENTS; |
| 1515 |
} |
| 1516 |
|
| 1517 |
status_t Color::parse4(const char *src) noexcept |
| 1518 |
{ |
| 1519 |
return (src != NULL) ? parse4(src, ::strlen(src)) : STATUS_BAD_ARGUMENTS; |
| 1520 |
} |
| 1521 |
|
| 1522 |
status_t Color::parse3(const char *src) noexcept |
| 1523 |
{ |
| 1524 |
return (src != NULL) ? parse3(src, ::strlen(src)) : STATUS_BAD_ARGUMENTS; |
| 1525 |
} |
| 1526 |
|
| 1527 |
status_t Color::parse(const char *src) noexcept |
| 1528 |
{ |
| 1529 |
return (src != NULL) ? parse(src, ::strlen(src)) : STATUS_BAD_ARGUMENTS; |
| 1530 |
} |
| 1531 |
|
| 1532 |
uint32_t Color::rgb24() const noexcept |
| 1533 |
{ |
| 1534 |
calc_rgb(); |
| 1535 |
return |
| 1536 |
(uint32_t(rgb.R * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 16) | |
| 1537 |
(uint32_t(rgb.G * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 8) | |
| 1538 |
(uint32_t(rgb.B * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 0); |
| 1539 |
} |
| 1540 |
|
| 1541 |
uint32_t Color::rgba32() const noexcept |
| 1542 |
{ |
| 1543 |
calc_rgb(); |
| 1544 |
return |
| 1545 |
(uint32_t(A * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 24) | |
| 1546 |
(uint32_t(rgb.R * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 16) | |
| 1547 |
(uint32_t(rgb.G * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 8) | |
| 1548 |
(uint32_t(rgb.B * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 0); |
| 1549 |
} |
| 1550 |
|
| 1551 |
uint32_t Color::dev_rgba32() const noexcept |
| 1552 |
{ |
| 1553 |
calc_rgb(); |
| 1554 |
return |
| 1555 |
(uint32_t((1.0f - A) * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 24) | |
| 1556 |
(uint32_t(rgb.R * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 16) | |
| 1557 |
(uint32_t(rgb.G * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 8) | |
| 1558 |
(uint32_t(rgb.B * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 0); |
| 1559 |
} |
| 1560 |
|
| 1561 |
uint32_t Color::prgba32() const noexcept |
| 1562 |
{ |
| 1563 |
calc_rgb(); |
| 1564 |
const float a = 1.0f - A; |
| 1565 |
return |
| 1566 |
(uint32_t(a * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 24) | |
| 1567 |
(uint32_t(rgb.R * a * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 16) | |
| 1568 |
(uint32_t(rgb.G * a * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 8) | |
| 1569 |
(uint32_t(rgb.B * a * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 0); |
| 1570 |
} |
| 1571 |
|
| 1572 |
uint32_t Color::hsl24() const noexcept |
| 1573 |
{ |
| 1574 |
calc_hsl(); |
| 1575 |
return |
| 1576 |
(uint32_t(hsl.H * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 16) | |
| 1577 |
(uint32_t(hsl.S * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 8) | |
| 1578 |
(uint32_t(hsl.L * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 0); |
| 1579 |
} |
| 1580 |
|
| 1581 |
uint32_t Color::hsla32() const noexcept |
| 1582 |
{ |
| 1583 |
calc_hsl(); |
| 1584 |
return |
| 1585 |
(uint32_t(A * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 24) | |
| 1586 |
(uint32_t(hsl.H * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 16) | |
| 1587 |
(uint32_t(hsl.S * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 8) | |
| 1588 |
(uint32_t(hsl.L * FLOAT_COL_COEFF + FLOAT_COL_BIAS) << 0); |
| 1589 |
} |
| 1590 |
|
| 1591 |
const Color &Color::get_xyz(float &x, float &y, float &z) const noexcept |
| 1592 |
{ |
| 1593 |
calc_xyz(); |
| 1594 |
x = xyz.X; |
| 1595 |
y = xyz.Y; |
| 1596 |
z = xyz.Z; |
| 1597 |
|
| 1598 |
return *this; |
| 1599 |
} |
| 1600 |
|
| 1601 |
Color &Color::get_xyz(float &x, float &y, float &z) noexcept |
| 1602 |
{ |
| 1603 |
calc_xyz(); |
| 1604 |
x = xyz.X; |
| 1605 |
y = xyz.Y; |
| 1606 |
z = xyz.Z; |
| 1607 |
|
| 1608 |
return *this; |
| 1609 |
} |
| 1610 |
|
| 1611 |
const Color &Color::get_xyza(float &x, float &y, float &z, float &a) const noexcept |
| 1612 |
{ |
| 1613 |
calc_xyz(); |
| 1614 |
x = xyz.X; |
| 1615 |
y = xyz.Y; |
| 1616 |
z = xyz.Z; |
| 1617 |
a = clamp(A); |
| 1618 |
|
| 1619 |
return *this; |
| 1620 |
} |
| 1621 |
|
| 1622 |
Color &Color::get_xyza(float &x, float &y, float &z, float &a) noexcept |
| 1623 |
{ |
| 1624 |
calc_xyz(); |
| 1625 |
x = xyz.X; |
| 1626 |
y = xyz.Y; |
| 1627 |
z = xyz.Z; |
| 1628 |
a = clamp(A); |
| 1629 |
|
| 1630 |
return *this; |
| 1631 |
} |
| 1632 |
|
| 1633 |
Color &Color::xyz_x(float x) noexcept |
| 1634 |
{ |
| 1635 |
calc_xyz().X = x; |
| 1636 |
mask = M_XYZ; |
| 1637 |
return *this; |
| 1638 |
} |
| 1639 |
|
| 1640 |
Color &Color::xyz_y(float y) noexcept |
| 1641 |
{ |
| 1642 |
calc_xyz().Y = y; |
| 1643 |
mask = M_XYZ; |
| 1644 |
return *this; |
| 1645 |
} |
| 1646 |
|
| 1647 |
Color &Color::xyz_z(float z) noexcept |
| 1648 |
{ |
| 1649 |
calc_xyz().Z = z; |
| 1650 |
mask = M_XYZ; |
| 1651 |
return *this; |
| 1652 |
} |
| 1653 |
|
| 1654 |
Color &Color::set_xyz(float x, float y, float z) noexcept |
| 1655 |
{ |
| 1656 |
xyz.X = lsp_limit(x, 0.0f, 100.0f); |
| 1657 |
xyz.Y = lsp_limit(y, 0.0f, 100.0f); |
| 1658 |
xyz.Z = lsp_limit(z, 0.0f, 110.0f); |
| 1659 |
mask = M_XYZ; |
| 1660 |
return *this; |
| 1661 |
} |
| 1662 |
|
| 1663 |
Color &Color::set_xyza(float x, float y, float z, float a) noexcept |
| 1664 |
{ |
| 1665 |
xyz.X = lsp_limit(x, 0.0f, 100.0f); |
| 1666 |
xyz.Y = lsp_limit(y, 0.0f, 100.0f); |
| 1667 |
xyz.Z = lsp_limit(z, 0.0f, 110.0f); |
| 1668 |
A = clamp(a); |
| 1669 |
mask = M_XYZ; |
| 1670 |
return *this; |
| 1671 |
} |
| 1672 |
|
| 1673 |
const Color &Color::get_lab(float &l, float &a, float &b) const noexcept |
| 1674 |
{ |
| 1675 |
calc_lab(); |
| 1676 |
l = lab.L; |
| 1677 |
a = lab.A; |
| 1678 |
b = lab.B; |
| 1679 |
return *this; |
| 1680 |
} |
| 1681 |
|
| 1682 |
Color &Color::get_lab(float &l, float &a, float &b) noexcept |
| 1683 |
{ |
| 1684 |
calc_lab(); |
| 1685 |
l = lab.L; |
| 1686 |
a = lab.A; |
| 1687 |
b = lab.B; |
| 1688 |
return *this; |
| 1689 |
} |
| 1690 |
|
| 1691 |
const Color &Color::get_laba(float &l, float &a, float &b, float &alpha) const noexcept |
| 1692 |
{ |
| 1693 |
calc_lab(); |
| 1694 |
l = lab.L; |
| 1695 |
a = lab.A; |
| 1696 |
b = lab.B; |
| 1697 |
alpha = clamp(A); |
| 1698 |
return *this; |
| 1699 |
} |
| 1700 |
|
| 1701 |
Color &Color::get_laba(float &l, float &a, float &b, float &alpha) noexcept |
| 1702 |
{ |
| 1703 |
calc_lab(); |
| 1704 |
l = lab.L; |
| 1705 |
a = lab.A; |
| 1706 |
b = lab.B; |
| 1707 |
alpha = clamp(A); |
| 1708 |
return *this; |
| 1709 |
} |
| 1710 |
|
| 1711 |
Color &Color::lab_l(float l) noexcept |
| 1712 |
{ |
| 1713 |
calc_lab().L = l; |
| 1714 |
mask = M_LAB; |
| 1715 |
return *this; |
| 1716 |
} |
| 1717 |
|
| 1718 |
Color &Color::lab_a(float a) noexcept |
| 1719 |
{ |
| 1720 |
calc_lab().A = a; |
| 1721 |
mask = M_LAB; |
| 1722 |
return *this; |
| 1723 |
} |
| 1724 |
|
| 1725 |
Color &Color::lab_b(float b) noexcept |
| 1726 |
{ |
| 1727 |
calc_lab().B = b; |
| 1728 |
mask = M_LAB; |
| 1729 |
return *this; |
| 1730 |
} |
| 1731 |
|
| 1732 |
Color &Color::set_lab(float l, float a, float b) noexcept |
| 1733 |
{ |
| 1734 |
lab.L = l; |
| 1735 |
lab.A = a; |
| 1736 |
lab.B = b; |
| 1737 |
mask = M_LAB; |
| 1738 |
return *this; |
| 1739 |
} |
| 1740 |
|
| 1741 |
Color &Color::set_laba(float l, float a, float b, float alpha) noexcept |
| 1742 |
{ |
| 1743 |
lab.L = l; |
| 1744 |
lab.A = a; |
| 1745 |
lab.B = b; |
| 1746 |
A = clamp(alpha); |
| 1747 |
mask = M_LAB; |
| 1748 |
return *this; |
| 1749 |
} |
| 1750 |
|
| 1751 |
// Check that LCH data is currently present without need of implicit conversion |
| 1752 |
const Color &Color::get_lch(float &l, float &c, float &h) const noexcept |
| 1753 |
{ |
| 1754 |
calc_lch(); |
| 1755 |
l = lch.L; |
| 1756 |
c = lch.C; |
| 1757 |
h = lch.H; |
| 1758 |
return *this; |
| 1759 |
} |
| 1760 |
|
| 1761 |
Color &Color::get_lch(float &l, float &c, float &h) noexcept |
| 1762 |
{ |
| 1763 |
calc_lch(); |
| 1764 |
l = lch.L; |
| 1765 |
c = lch.C; |
| 1766 |
h = lch.H; |
| 1767 |
return *this; |
| 1768 |
} |
| 1769 |
|
| 1770 |
const Color &Color::get_lcha(float &l, float &c, float &h, float &alpha) const noexcept |
| 1771 |
{ |
| 1772 |
calc_lch(); |
| 1773 |
l = lch.L; |
| 1774 |
c = lch.C; |
| 1775 |
h = lch.H; |
| 1776 |
alpha = A; |
| 1777 |
return *this; |
| 1778 |
} |
| 1779 |
|
| 1780 |
Color &Color::get_lcha(float &l, float &c, float &h, float &alpha) noexcept |
| 1781 |
{ |
| 1782 |
calc_lch(); |
| 1783 |
l = lch.L; |
| 1784 |
c = lch.C; |
| 1785 |
h = lch.H; |
| 1786 |
alpha = A; |
| 1787 |
return *this; |
| 1788 |
} |
| 1789 |
|
| 1790 |
Color &Color::lch_l(float l) noexcept |
| 1791 |
{ |
| 1792 |
calc_lch().L = l; |
| 1793 |
mask = M_LCH; |
| 1794 |
return *this; |
| 1795 |
} |
| 1796 |
|
| 1797 |
Color &Color::lch_c(float c) noexcept |
| 1798 |
{ |
| 1799 |
calc_lch().C = c; |
| 1800 |
mask = M_LCH; |
| 1801 |
return *this; |
| 1802 |
} |
| 1803 |
|
| 1804 |
Color &Color::lch_h(float h) noexcept |
| 1805 |
{ |
| 1806 |
calc_lch().H = h; |
| 1807 |
mask = M_LCH; |
| 1808 |
return *this; |
| 1809 |
} |
| 1810 |
|
| 1811 |
Color &Color::set_lch(float l, float c, float h) noexcept |
| 1812 |
{ |
| 1813 |
lch.L = l; |
| 1814 |
lch.C = c; |
| 1815 |
lch.H = h; |
| 1816 |
mask = M_LCH; |
| 1817 |
return *this; |
| 1818 |
} |
| 1819 |
|
| 1820 |
Color &Color::set_lcha(float l, float c, float h, float alpha) noexcept |
| 1821 |
{ |
| 1822 |
lch.L = l; |
| 1823 |
lch.C = c; |
| 1824 |
lch.H = h; |
| 1825 |
A = clamp(alpha); |
| 1826 |
mask = M_LCH; |
| 1827 |
return *this; |
| 1828 |
} |
| 1829 |
|
| 1830 |
const Color &Color::get_hcl(float &h, float &c, float &l) const noexcept |
| 1831 |
{ |
| 1832 |
calc_lch(); |
| 1833 |
l = lch.L; |
| 1834 |
c = lch.C; |
| 1835 |
h = lch.H; |
| 1836 |
return *this; |
| 1837 |
} |
| 1838 |
|
| 1839 |
Color &Color::get_hcl(float &h, float &c, float &l) noexcept |
| 1840 |
{ |
| 1841 |
calc_lch(); |
| 1842 |
l = lch.L; |
| 1843 |
c = lch.C; |
| 1844 |
h = lch.H; |
| 1845 |
return *this; |
| 1846 |
} |
| 1847 |
|
| 1848 |
const Color &Color::get_hcla(float &h, float &c, float &l, float &alpha) const noexcept |
| 1849 |
{ |
| 1850 |
calc_lch(); |
| 1851 |
l = lch.L; |
| 1852 |
c = lch.C; |
| 1853 |
h = lch.H; |
| 1854 |
alpha = clamp(A); |
| 1855 |
return *this; |
| 1856 |
} |
| 1857 |
|
| 1858 |
Color &Color::get_hcla(float &h, float &c, float &l, float &alpha) noexcept |
| 1859 |
{ |
| 1860 |
calc_lch(); |
| 1861 |
l = lch.L; |
| 1862 |
c = lch.C; |
| 1863 |
h = lch.H; |
| 1864 |
alpha = clamp(A); |
| 1865 |
return *this; |
| 1866 |
} |
| 1867 |
|
| 1868 |
Color &Color::hcl_l(float l) noexcept |
| 1869 |
{ |
| 1870 |
calc_lch().L = l; |
| 1871 |
mask = M_LCH; |
| 1872 |
return *this; |
| 1873 |
} |
| 1874 |
|
| 1875 |
Color &Color::hcl_c(float c) noexcept |
| 1876 |
{ |
| 1877 |
calc_lch().C = c; |
| 1878 |
mask = M_LCH; |
| 1879 |
return *this; |
| 1880 |
} |
| 1881 |
|
| 1882 |
Color &Color::hcl_h(float h) noexcept |
| 1883 |
{ |
| 1884 |
calc_lch().H = h; |
| 1885 |
mask = M_LCH; |
| 1886 |
return *this; |
| 1887 |
} |
| 1888 |
|
| 1889 |
Color &Color::set_hcl(float h, float c, float l) noexcept |
| 1890 |
{ |
| 1891 |
lch.L = l; |
| 1892 |
lch.C = c; |
| 1893 |
lch.H = h; |
| 1894 |
mask = M_LCH; |
| 1895 |
return *this; |
| 1896 |
} |
| 1897 |
|
| 1898 |
Color &Color::set_hcla(float h, float c, float l, float alpha) noexcept |
| 1899 |
{ |
| 1900 |
lch.L = l; |
| 1901 |
lch.C = c; |
| 1902 |
lch.H = h; |
| 1903 |
A = alpha; |
| 1904 |
mask = M_LCH; |
| 1905 |
return *this; |
| 1906 |
} |
| 1907 |
|
| 1908 |
const Color &Color::get_cmyk(float &c, float &m, float &y, float &k) const noexcept |
| 1909 |
{ |
| 1910 |
calc_cmyk(); |
| 1911 |
c = cmyk.C; |
| 1912 |
m = cmyk.M; |
| 1913 |
y = cmyk.Y; |
| 1914 |
k = cmyk.K; |
| 1915 |
return *this; |
| 1916 |
} |
| 1917 |
|
| 1918 |
Color &Color::get_cmyk(float &c, float &m, float &y, float &k) noexcept |
| 1919 |
{ |
| 1920 |
calc_cmyk(); |
| 1921 |
c = cmyk.C; |
| 1922 |
m = cmyk.M; |
| 1923 |
y = cmyk.Y; |
| 1924 |
k = cmyk.K; |
| 1925 |
return *this; |
| 1926 |
} |
| 1927 |
|
| 1928 |
const Color &Color::get_cmyka(float &c, float &m, float &y, float &k, float &alpha) const noexcept |
| 1929 |
{ |
| 1930 |
calc_cmyk(); |
| 1931 |
c = cmyk.C; |
| 1932 |
m = cmyk.M; |
| 1933 |
y = cmyk.Y; |
| 1934 |
k = cmyk.K; |
| 1935 |
alpha = clamp(A); |
| 1936 |
return *this; |
| 1937 |
} |
| 1938 |
|
| 1939 |
Color &Color::get_cmyka(float &c, float &m, float &y, float &k, float &alpha) noexcept |
| 1940 |
{ |
| 1941 |
calc_cmyk(); |
| 1942 |
c = cmyk.C; |
| 1943 |
m = cmyk.M; |
| 1944 |
y = cmyk.Y; |
| 1945 |
k = cmyk.K; |
| 1946 |
alpha = clamp(A); |
| 1947 |
return *this; |
| 1948 |
} |
| 1949 |
|
| 1950 |
Color &Color::cyan(float c) noexcept |
| 1951 |
{ |
| 1952 |
calc_cmyk().C = clamp(c); |
| 1953 |
mask = M_CMYK; |
| 1954 |
return *this; |
| 1955 |
} |
| 1956 |
|
| 1957 |
Color &Color::magenta(float m) noexcept |
| 1958 |
{ |
| 1959 |
calc_cmyk().M = clamp(m); |
| 1960 |
mask = M_CMYK; |
| 1961 |
return *this; |
| 1962 |
} |
| 1963 |
|
| 1964 |
Color &Color::yellow(float y) noexcept |
| 1965 |
{ |
| 1966 |
calc_cmyk().Y = clamp(y); |
| 1967 |
mask = M_CMYK; |
| 1968 |
return *this; |
| 1969 |
} |
| 1970 |
|
| 1971 |
Color &Color::black(float k) noexcept |
| 1972 |
{ |
| 1973 |
calc_cmyk().K = clamp(k); |
| 1974 |
mask = M_CMYK; |
| 1975 |
return *this; |
| 1976 |
} |
| 1977 |
|
| 1978 |
Color &Color::set_cmyk(float c, float m, float y, float k) noexcept |
| 1979 |
{ |
| 1980 |
cmyk.C = clamp(c); |
| 1981 |
cmyk.M = clamp(m); |
| 1982 |
cmyk.Y = clamp(y); |
| 1983 |
cmyk.K = clamp(k); |
| 1984 |
mask = M_CMYK; |
| 1985 |
return *this; |
| 1986 |
} |
| 1987 |
|
| 1988 |
Color &Color::set_cmyka(float c, float m, float y, float k, float alpha) noexcept |
| 1989 |
{ |
| 1990 |
cmyk.C = clamp(c); |
| 1991 |
cmyk.M = clamp(m); |
| 1992 |
cmyk.Y = clamp(y); |
| 1993 |
cmyk.K = clamp(k); |
| 1994 |
A = clamp(alpha); |
| 1995 |
mask = M_CMYK; |
| 1996 |
return *this; |
| 1997 |
} |
| 1998 |
|
| 1999 |
} /* namespace lsp */ |