| 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 |
#ifndef LSP_PLUG_IN_RUNTIME_COLOR_H_ |
| 23 |
#define LSP_PLUG_IN_RUNTIME_COLOR_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/common/types.h> |
| 26 |
#include <lsp-plug.in/common/status.h> |
| 27 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 28 |
|
| 29 |
namespace lsp |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Color class. Represents an object that allows to work with different color models. |
| 33 |
* Supported color models: |
| 34 |
* - RGB (red, green, blue components) |
| 35 |
* - HSL (hue, saturation, lightness components) |
| 36 |
* - Alpha channel |
| 37 |
* All components are normalzed within the [0.0f .. 1.0f] range |
| 38 |
*/ |
| 39 |
class Color |
| 40 |
{ |
| 41 |
protected: |
| 42 |
enum mask_t |
| 43 |
{ |
| 44 |
M_RGB = 1 << 0, // Standard RGB color components |
| 45 |
M_HSL = 1 << 1, // Hue/Saturation/Lightness components |
| 46 |
M_XYZ = 1 << 2, // CIE XYZ components |
| 47 |
M_LAB = 1 << 3, // CIE LAB D65 standard components |
| 48 |
M_LCH = 1 << 4, // CIE LCH D65 standard components |
| 49 |
M_CMYK = 1 << 5, // CMYK color space |
| 50 |
}; |
| 51 |
|
| 52 |
typedef struct rgb_t |
| 53 |
{ |
| 54 |
float R, G, B; |
| 55 |
} rgb_t; |
| 56 |
|
| 57 |
typedef struct hsl_t |
| 58 |
{ |
| 59 |
float H, S, L; |
| 60 |
} hsl_t; |
| 61 |
|
| 62 |
typedef struct xyz_t |
| 63 |
{ |
| 64 |
float X, Y, Z; |
| 65 |
} xyz_t; |
| 66 |
|
| 67 |
typedef struct lab_t |
| 68 |
{ |
| 69 |
float L, A, B; |
| 70 |
} lab_t; |
| 71 |
|
| 72 |
typedef struct lch_t |
| 73 |
{ |
| 74 |
float L, C, H; |
| 75 |
} lch_t; |
| 76 |
|
| 77 |
typedef struct cmyk_t |
| 78 |
{ |
| 79 |
float C, M, Y, K; |
| 80 |
} cmyk_t; |
| 81 |
|
| 82 |
protected: |
| 83 |
mutable rgb_t rgb; |
| 84 |
mutable hsl_t hsl; |
| 85 |
mutable xyz_t xyz; |
| 86 |
mutable lab_t lab; |
| 87 |
mutable lch_t lch; |
| 88 |
mutable cmyk_t cmyk; |
| 89 |
mutable size_t mask; |
| 90 |
mutable float A; |
| 91 |
|
| 92 |
rgb_t &calc_rgb() const noexcept; |
| 93 |
hsl_t &calc_hsl() const noexcept; |
| 94 |
xyz_t &calc_xyz() const noexcept; |
| 95 |
lab_t &calc_lab() const noexcept; |
| 96 |
lch_t &calc_lch() const noexcept; |
| 97 |
cmyk_t &calc_cmyk() const noexcept; |
| 98 |
|
| 99 |
bool hsl_to_rgb() const noexcept; |
| 100 |
bool xyz_to_rgb() const noexcept; |
| 101 |
bool lab_to_xyz() const noexcept; |
| 102 |
bool lch_to_lab() const noexcept; |
| 103 |
bool cmyk_to_rgb() const noexcept; |
| 104 |
|
| 105 |
protected: |
| 106 |
static status_t parse_hex(float *dst, size_t n, char prefix, const char *src, size_t len) noexcept; |
| 107 |
static status_t parse_numeric(float *dst, size_t nmin, size_t nmax, const char *prefix, const char *src, size_t len) noexcept; |
| 108 |
static status_t parse_cnumeric(float *dst, size_t nmin, size_t nmax, const char *prefix, const char *src, size_t len) noexcept; |
| 109 |
static ssize_t format(char *dst, size_t len, size_t tolerance, const float *v, char prefix, bool alpha) noexcept; |
| 110 |
static inline float clamp(float x) noexcept; |
| 111 |
|
| 112 |
// Construction and destruction |
| 113 |
public: |
| 114 |
Color() noexcept; |
| 115 |
explicit Color(const Color &src) noexcept; |
| 116 |
explicit Color(const Color *src) noexcept; |
| 117 |
Color(Color &&src) noexcept; |
| 118 |
explicit Color(uint32_t rgb) noexcept; |
| 119 |
|
| 120 |
Color(float r, float g, float b) noexcept; |
| 121 |
Color(float r, float g, float b, float a) noexcept; |
| 122 |
Color(const Color &src, float a) noexcept; |
| 123 |
Color(const Color *src, float a) noexcept; |
| 124 |
Color(uint32_t rgb, float a) noexcept; |
| 125 |
|
| 126 |
Color &operator = (const Color & src) noexcept; |
| 127 |
Color &operator = (Color && src) noexcept; |
| 128 |
|
| 129 |
// Copying and swapping |
| 130 |
public: |
| 131 |
Color ©(const Color &c) noexcept; |
| 132 |
Color ©(const Color *c) noexcept; |
| 133 |
Color ©(const Color &c, float a) noexcept; |
| 134 |
Color ©(const Color *c, float a) noexcept; |
| 135 |
void swap(Color *src) noexcept; |
| 136 |
inline void swap(Color &src) noexcept { swap(&src); } |
| 137 |
|
| 138 |
// RGB-related functions |
| 139 |
public: |
| 140 |
// Check that RGB data is currently present without need of implicit conversion |
| 141 |
inline bool is_rgb() const noexcept { return mask & M_RGB; } |
| 142 |
|
| 143 |
// Get color components |
| 144 |
inline float red() const noexcept { return calc_rgb().R; } |
| 145 |
inline float green() const noexcept { return calc_rgb().G; } |
| 146 |
inline float blue() const noexcept { return calc_rgb().B; } |
| 147 |
|
| 148 |
const Color &get_rgb(float &r, float &g, float &b) const noexcept; |
| 149 |
Color &get_rgb(float &r, float &g, float &b) noexcept; |
| 150 |
const Color &get_rgba(float &r, float &g, float &b, float &a) const noexcept; |
| 151 |
const Color &get_rgbo(float &r, float &g, float &b, float &o) const noexcept; |
| 152 |
Color &get_rgba(float &r, float &g, float &b, float &a) noexcept; |
| 153 |
|
| 154 |
uint32_t rgb24() const noexcept; |
| 155 |
|
| 156 |
/** |
| 157 |
* Get RGBA color where fully transparent alpha is 0xff and fully |
| 158 |
* opaque alpha is 0xff. |
| 159 |
* @return RGBA32 color in format 0xAARRGGBB |
| 160 |
*/ |
| 161 |
uint32_t rgba32() const noexcept; |
| 162 |
|
| 163 |
/** |
| 164 |
* Get device RGBA color where fully transparent alpha is 0 |
| 165 |
* and fully opaqe alpha is 0xff. |
| 166 |
* @return RGBA32 color in device format 0xAARRGGBB |
| 167 |
*/ |
| 168 |
uint32_t dev_rgba32() const noexcept; |
| 169 |
|
| 170 |
/** |
| 171 |
* Get pre-multiplied RGBA color where fully transparent alpha is 0 |
| 172 |
* and fully opaqe alpha is 0xff. |
| 173 |
* @return premultiplied RGBA32 color in format 0xAARRGGBB |
| 174 |
*/ |
| 175 |
uint32_t prgba32() const noexcept; |
| 176 |
|
| 177 |
// Update color components |
| 178 |
Color &red(float r) noexcept; |
| 179 |
Color &green(float g) noexcept; |
| 180 |
Color &blue(float b) noexcept; |
| 181 |
|
| 182 |
Color &set_rgb(float r, float g, float b) noexcept; |
| 183 |
Color &set_rgba(float r, float g, float b, float a) noexcept; |
| 184 |
|
| 185 |
Color &set_rgb24(uint32_t v) noexcept; |
| 186 |
Color &set_rgba32(uint32_t v) noexcept; |
| 187 |
Color &set_prgba32(uint32_t v) noexcept; |
| 188 |
Color &set_dev_rgba32(uint32_t v) noexcept; |
| 189 |
|
| 190 |
// Formatting |
| 191 |
ssize_t format_rgb(char *dst, size_t len, size_t tolerance = 2) const noexcept; |
| 192 |
ssize_t format_rgb(LSPString *dst, size_t tolerance = 2) const noexcept; |
| 193 |
ssize_t format_rgba(char *dst, size_t len, size_t tolerance = 2) const noexcept; |
| 194 |
ssize_t format_rgba(LSPString *dst, size_t tolerance = 2) const noexcept; |
| 195 |
|
| 196 |
// Parsing |
| 197 |
status_t parse_rgb(const char *src, size_t len) noexcept; |
| 198 |
status_t parse_rgb(const char *src) noexcept; |
| 199 |
status_t parse_rgb(const LSPString *src, size_t len) noexcept { return parse_rgb(src->get_utf8(0, len)); } |
| 200 |
status_t parse_rgb(const LSPString *src) noexcept { return parse_rgb(src->get_utf8()); } |
| 201 |
|
| 202 |
status_t parse_rgba(const char *src, size_t len) noexcept; |
| 203 |
status_t parse_rgba(const char *src) noexcept; |
| 204 |
status_t parse_rgba(const LSPString *src, size_t len) noexcept { return parse_rgba(src->get_utf8(0, len)); } |
| 205 |
status_t parse_rgba(const LSPString *src) noexcept { return parse_rgba(src->get_utf8()); } |
| 206 |
|
| 207 |
// HSL-related functions |
| 208 |
public: |
| 209 |
// Check that HSL data is currently present without need of implicit conversion |
| 210 |
inline bool is_hsl() const noexcept { return mask & M_HSL; } |
| 211 |
|
| 212 |
// Get color components |
| 213 |
inline float hue() const noexcept { return calc_hsl().H; } |
| 214 |
inline float saturation() const noexcept { return calc_hsl().S; } |
| 215 |
inline float lightness() const noexcept { return calc_hsl().L; } |
| 216 |
inline float hsl_hue() const noexcept { return calc_hsl().H; } |
| 217 |
inline float hsl_saturation() const noexcept { return calc_hsl().S; } |
| 218 |
inline float hsl_lightness() const noexcept { return calc_hsl().L; } |
| 219 |
|
| 220 |
const Color &get_hsl(float &h, float &s, float &l) const noexcept; |
| 221 |
Color &get_hsl(float &h, float &s, float &l) noexcept; |
| 222 |
const Color &get_hsla(float &h, float &s, float &l, float &a) const noexcept; |
| 223 |
Color &get_hsla(float &h, float &s, float &l, float &a) noexcept; |
| 224 |
|
| 225 |
uint32_t hsl24() const noexcept; |
| 226 |
uint32_t hsla32() const noexcept; |
| 227 |
|
| 228 |
// Update color components |
| 229 |
Color &hue(float h) noexcept; |
| 230 |
Color &saturation(float s) noexcept; |
| 231 |
Color &lightness(float l) noexcept; |
| 232 |
Color &hsl_hue(float h) noexcept; |
| 233 |
Color &hsl_saturation(float s) noexcept; |
| 234 |
Color &hsl_lightness(float l) noexcept; |
| 235 |
|
| 236 |
Color &set_hsl(float h, float s, float l) noexcept; |
| 237 |
Color &set_hsla(float h, float s, float l, float a) noexcept; |
| 238 |
|
| 239 |
Color &set_hsl24(uint32_t v) noexcept; |
| 240 |
Color &set_hsla32(uint32_t v) noexcept; |
| 241 |
|
| 242 |
// Formatting |
| 243 |
ssize_t format_hsl(char *dst, size_t len, size_t tolerance = 2) const noexcept; |
| 244 |
ssize_t format_hsl(LSPString *dst, size_t tolerance = 2) const noexcept; |
| 245 |
ssize_t format_hsla(char *dst, size_t len, size_t tolerance = 2) const noexcept; |
| 246 |
ssize_t format_hsla(LSPString *dst, size_t tolerance = 2) const noexcept; |
| 247 |
|
| 248 |
// Parsing |
| 249 |
status_t parse_hsl(const char *src, size_t len) noexcept; |
| 250 |
status_t parse_hsl(const char *src) noexcept; |
| 251 |
status_t parse_hsl(const LSPString *src, size_t len) noexcept { return parse_hsl(src->get_utf8(0, len)); } |
| 252 |
status_t parse_hsl(const LSPString *src) noexcept { return parse_hsl(src->get_utf8()); } |
| 253 |
|
| 254 |
status_t parse_hsla(const char *src, size_t len) noexcept; |
| 255 |
status_t parse_hsla(const char *src) noexcept; |
| 256 |
status_t parse_hsla(const LSPString *src, size_t len) noexcept { return parse_hsla(src->get_utf8(0, len)); } |
| 257 |
status_t parse_hsla(const LSPString *src) noexcept { return parse_hsla(src->get_utf8()); } |
| 258 |
|
| 259 |
// CIE XYZ-related functions (D65 standard) |
| 260 |
public: |
| 261 |
// Check that XYZ data is currently present without need of implicit conversion |
| 262 |
inline bool is_xyz() const noexcept { return mask & M_XYZ; } |
| 263 |
|
| 264 |
// Get color components |
| 265 |
inline float xyz_x() const noexcept { return calc_xyz().X; } |
| 266 |
inline float xyz_y() const noexcept { return calc_xyz().Y; } |
| 267 |
inline float xyz_z() const noexcept { return calc_xyz().Z; } |
| 268 |
|
| 269 |
const Color &get_xyz(float &x, float &y, float &z) const noexcept; |
| 270 |
Color &get_xyz(float &x, float &y, float &z) noexcept; |
| 271 |
const Color &get_xyza(float &x, float &y, float &z, float &a) const noexcept; |
| 272 |
Color &get_xyza(float &x, float &y, float &z, float &a) noexcept; |
| 273 |
|
| 274 |
// Update color components |
| 275 |
Color &xyz_x(float x) noexcept; |
| 276 |
Color &xyz_y(float y) noexcept; |
| 277 |
Color &xyz_z(float z) noexcept; |
| 278 |
|
| 279 |
Color &set_xyz(float x, float y, float z) noexcept; |
| 280 |
Color &set_xyza(float x, float y, float z, float a) noexcept; |
| 281 |
|
| 282 |
// CIE LAB-related functions (D65 standard) |
| 283 |
public: |
| 284 |
// Check that LAB data is currently present without need of implicit conversion |
| 285 |
inline bool is_lab() const noexcept { return mask & M_LAB; } |
| 286 |
|
| 287 |
// Get color components |
| 288 |
inline float lab_l() const noexcept { return calc_lab().L; } |
| 289 |
inline float lab_a() const noexcept { return calc_lab().A; } |
| 290 |
inline float lab_b() const noexcept { return calc_lab().B; } |
| 291 |
|
| 292 |
const Color &get_lab(float &l, float &a, float &b) const noexcept; |
| 293 |
Color &get_lab(float &l, float &a, float &b) noexcept; |
| 294 |
const Color &get_laba(float &l, float &a, float &b, float &alpha) const noexcept; |
| 295 |
Color &get_laba(float &l, float &a, float &b, float &alpha) noexcept; |
| 296 |
|
| 297 |
// Update color components |
| 298 |
Color &lab_l(float l) noexcept; |
| 299 |
Color &lab_a(float a) noexcept; |
| 300 |
Color &lab_b(float b) noexcept; |
| 301 |
|
| 302 |
Color &set_lab(float l, float a, float b) noexcept; |
| 303 |
Color &set_laba(float l, float a, float b, float alpha) noexcept; |
| 304 |
|
| 305 |
// CIE LCH-related functions (D65 standard) |
| 306 |
public: |
| 307 |
// Check that LCH data is currently present without need of implicit conversion |
| 308 |
inline bool is_lch() const noexcept { return mask & M_LCH; } |
| 309 |
inline bool is_hcl() const noexcept { return mask & M_LCH; } |
| 310 |
|
| 311 |
// Get color components |
| 312 |
inline float lch_l() const noexcept { return calc_lch().L; } |
| 313 |
inline float lch_c() const noexcept { return calc_lch().C; } |
| 314 |
inline float lch_h() const noexcept { return calc_lch().H; } |
| 315 |
inline float hcl_l() const noexcept { return calc_lch().L; } |
| 316 |
inline float hcl_c() const noexcept { return calc_lch().C; } |
| 317 |
inline float hcl_h() const noexcept { return calc_lch().H; } |
| 318 |
|
| 319 |
const Color &get_lch(float &l, float &c, float &h) const noexcept; |
| 320 |
Color &get_lch(float &l, float &c, float &h) noexcept; |
| 321 |
const Color &get_lcha(float &l, float &c, float &h, float &alpha) const noexcept; |
| 322 |
Color &get_lcha(float &l, float &c, float &h, float &alpha) noexcept; |
| 323 |
const Color &get_hcl(float &h, float &c, float &l) const noexcept; |
| 324 |
Color &get_hcl(float &h, float &c, float &l) noexcept; |
| 325 |
const Color &get_hcla(float &h, float &c, float &l, float &alpha) const noexcept; |
| 326 |
Color &get_hcla(float &h, float &c, float &l, float &alpha) noexcept; |
| 327 |
|
| 328 |
// Update color components |
| 329 |
Color &lch_l(float l) noexcept; |
| 330 |
Color &lch_c(float c) noexcept; |
| 331 |
Color &lch_h(float h) noexcept; |
| 332 |
Color &hcl_l(float l) noexcept; |
| 333 |
Color &hcl_c(float c) noexcept; |
| 334 |
Color &hcl_h(float h) noexcept; |
| 335 |
|
| 336 |
Color &set_lch(float l, float c, float h) noexcept; |
| 337 |
Color &set_lcha(float l, float c, float h, float alpha) noexcept; |
| 338 |
Color &set_hcl(float h, float c, float l) noexcept; |
| 339 |
Color &set_hcla(float h, float c, float l, float alpha) noexcept; |
| 340 |
|
| 341 |
// CMYK color space |
| 342 |
public: |
| 343 |
inline bool is_cmyk() const noexcept { return mask & M_CMYK; } |
| 344 |
inline float cyan() const noexcept { return calc_cmyk().C; } |
| 345 |
inline float magenta() const noexcept { return calc_cmyk().M; } |
| 346 |
inline float yellow() const noexcept { return calc_cmyk().Y; } |
| 347 |
inline float black() const noexcept { return calc_cmyk().K; } |
| 348 |
|
| 349 |
const Color &get_cmyk(float &c, float &m, float &y, float &k) const noexcept; |
| 350 |
Color &get_cmyk(float &c, float &m, float &y, float &k) noexcept; |
| 351 |
const Color &get_cmyka(float &c, float &m, float &y, float &k, float &alpha) const noexcept; |
| 352 |
Color &get_cmyka(float &c, float &m, float &y, float &k, float &alpha) noexcept; |
| 353 |
|
| 354 |
// Update color components |
| 355 |
Color &cyan(float c) noexcept; |
| 356 |
Color &magenta(float m) noexcept; |
| 357 |
Color &yellow(float y) noexcept; |
| 358 |
Color &black(float k) noexcept; |
| 359 |
|
| 360 |
Color &set_cmyk(float c, float m, float y, float k) noexcept; |
| 361 |
Color &set_cmyka(float c, float m, float y, float k, float alpha) noexcept; |
| 362 |
|
| 363 |
// Alpha-blending channel |
| 364 |
public: |
| 365 |
inline float alpha() const noexcept { return A; } |
| 366 |
inline float opacity() const noexcept { return 1.0f - A; } |
| 367 |
inline float dev_alpha() const noexcept { return 1.0f - A; } |
| 368 |
Color &alpha(float a) noexcept; |
| 369 |
Color &opacity(float o) noexcept; |
| 370 |
|
| 371 |
// Parsing |
| 372 |
public: |
| 373 |
// Parsing raw data |
| 374 |
status_t parse3(const char *src, size_t len) noexcept; |
| 375 |
status_t parse3(const char *src) noexcept; |
| 376 |
status_t parse4(const char *src, size_t len) noexcept; |
| 377 |
status_t parse4(const char *src) noexcept; |
| 378 |
status_t parse(const char *src, size_t len) noexcept; |
| 379 |
status_t parse(const char *src) noexcept; |
| 380 |
|
| 381 |
// Parsing LSPString |
| 382 |
inline status_t parse3(const LSPString *src, size_t len) noexcept { return parse3(src->get_utf8(0, len)); } |
| 383 |
inline status_t parse3(const LSPString *src) noexcept { return parse3(src->get_utf8()); } |
| 384 |
inline status_t parse4(const LSPString *src, size_t len) noexcept { return parse4(src->get_utf8(0, len)); } |
| 385 |
inline status_t parse4(const LSPString *src) noexcept { return parse4(src->get_utf8()); } |
| 386 |
|
| 387 |
// Formatting data |
| 388 |
ssize_t format3(char *dst, size_t len) const noexcept; |
| 389 |
ssize_t format3(LSPString *dst) const noexcept; |
| 390 |
ssize_t format4(char *dst, size_t len) const noexcept; |
| 391 |
ssize_t format4(LSPString *dst) const noexcept; |
| 392 |
|
| 393 |
// Miscellaneous effects |
| 394 |
public: |
| 395 |
Color &blend(const Color &c, float alpha) noexcept; |
| 396 |
Color &blend(float r, float g, float b, float alpha) noexcept; |
| 397 |
Color &darken(float amount) noexcept; |
| 398 |
Color &lighten(float amount) noexcept; |
| 399 |
Color &blend(const Color &c1, const Color &c2, float alpha) noexcept; |
| 400 |
void scale_hsl_lightness(float amount) noexcept; |
| 401 |
void scale_lch_luminance(float amount) noexcept; |
| 402 |
}; |
| 403 |
|
| 404 |
} /* namespace lsp */ |
| 405 |
|
| 406 |
#endif /* LSP_PLUG_IN_RUNTIME_COLOR_H_ */ |