| 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: 19 янв. 2026 г. |
| 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/version.h> |
| 23 |
|
| 24 |
#include <lsp-plug.in/fmt/xpm/ColorItem.h> |
| 25 |
#include <lsp-plug.in/common/alloc.h> |
| 26 |
#include <lsp-plug.in/stdlib/string.h> |
| 27 |
|
| 28 |
namespace lsp |
| 29 |
{ |
| 30 |
namespace xpm |
| 31 |
{ |
| 32 |
static constexpr float COLOR32_TO_FLOAT = 1.0f / 255.0f; |
| 33 |
static constexpr float COLOR64_TO_FLOAT = 1.0f / 65535.0f; |
| 34 |
|
| 35 |
ColorItem::ColorItem() noexcept |
| 36 |
{ |
| 37 |
nColor64 = 0; |
| 38 |
enState = STATE_UNSET; |
| 39 |
} |
| 40 |
|
| 41 |
ColorItem::ColorItem(const ColorItem & src) |
| 42 |
{ |
| 43 |
if (src.enState == STATE_NAME) |
| 44 |
{ |
| 45 |
nColor64 = 0; |
| 46 |
sName = (src.sName != NULL) ? strdup(src.sName) : NULL; |
| 47 |
} |
| 48 |
else |
| 49 |
nColor64 = 0; |
| 50 |
enState = src.enState; |
| 51 |
} |
| 52 |
|
| 53 |
ColorItem::ColorItem(ColorItem && src) noexcept |
| 54 |
{ |
| 55 |
nColor64 = src.nColor64; |
| 56 |
sName = src.sName; |
| 57 |
enState = src.enState; |
| 58 |
|
| 59 |
src.nColor64 = 0; |
| 60 |
src.enState = STATE_UNSET; |
| 61 |
} |
| 62 |
|
| 63 |
ColorItem::ColorItem(const char *id) |
| 64 |
{ |
| 65 |
nColor64 = 0; |
| 66 |
sName = (id != NULL) ? strdup(id) : NULL; |
| 67 |
enState = STATE_NAME; |
| 68 |
} |
| 69 |
|
| 70 |
ColorItem::ColorItem(uint32_t value) noexcept |
| 71 |
{ |
| 72 |
nColor64 = 0; |
| 73 |
nColor32 = value; |
| 74 |
enState = STATE_COLOR32; |
| 75 |
} |
| 76 |
|
| 77 |
ColorItem::ColorItem(uint64_t value) noexcept |
| 78 |
{ |
| 79 |
nColor64 = value; |
| 80 |
enState = STATE_COLOR64; |
| 81 |
} |
| 82 |
|
| 83 |
ColorItem::~ColorItem() |
| 84 |
{ |
| 85 |
if ((enState == STATE_NAME) && (sName != NULL)) |
| 86 |
{ |
| 87 |
free(sName); |
| 88 |
sName = NULL; |
| 89 |
} |
| 90 |
nColor64 = 0; |
| 91 |
enState = STATE_UNSET; |
| 92 |
} |
| 93 |
|
| 94 |
void ColorItem::clear_name() |
| 95 |
{ |
| 96 |
if ((enState == STATE_NAME) && (sName != NULL)) |
| 97 |
{ |
| 98 |
free(sName); |
| 99 |
sName = NULL; |
| 100 |
} |
| 101 |
nColor64 = 0; |
| 102 |
enState = STATE_NAME; |
| 103 |
} |
| 104 |
|
| 105 |
bool ColorItem::set_name(const char *name) |
| 106 |
{ |
| 107 |
if (name == NULL) |
| 108 |
{ |
| 109 |
clear_name(); |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
char *str = strdup(name); |
| 114 |
if (str == NULL) |
| 115 |
return false; |
| 116 |
|
| 117 |
if (enState == STATE_NAME) |
| 118 |
{ |
| 119 |
if (sName != NULL) |
| 120 |
free(sName); |
| 121 |
sName = str; |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
nColor64 = 0; |
| 126 |
sName = str; |
| 127 |
enState = STATE_NAME; |
| 128 |
} |
| 129 |
|
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
bool ColorItem::set_name(const char *name, size_t len) |
| 134 |
{ |
| 135 |
if (name == NULL) |
| 136 |
{ |
| 137 |
clear_name(); |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
char *str = strmemdup(name, len); |
| 142 |
if (str == NULL) |
| 143 |
return false; |
| 144 |
|
| 145 |
if (enState == STATE_NAME) |
| 146 |
{ |
| 147 |
if (sName != NULL) |
| 148 |
free(sName); |
| 149 |
sName = str; |
| 150 |
} |
| 151 |
else |
| 152 |
{ |
| 153 |
nColor64 = 0; |
| 154 |
sName = str; |
| 155 |
enState = STATE_NAME; |
| 156 |
} |
| 157 |
|
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
bool ColorItem::has_name(const char *name) const noexcept |
| 162 |
{ |
| 163 |
if (enState != STATE_NAME) |
| 164 |
return false; |
| 165 |
if (sName == name) // for NULL argument |
| 166 |
return true; |
| 167 |
if (sName == NULL) |
| 168 |
return false; |
| 169 |
return strcmp(sName, name) == 0; |
| 170 |
} |
| 171 |
|
| 172 |
uint32_t ColorItem::rgb24() const noexcept |
| 173 |
{ |
| 174 |
switch (enState) |
| 175 |
{ |
| 176 |
case STATE_COLOR32: |
| 177 |
return uint32_t(nColor32); |
| 178 |
|
| 179 |
case STATE_COLOR64: |
| 180 |
{ |
| 181 |
return |
| 182 |
(uint32_t(nColor64 >> 24) & 0xff0000) | |
| 183 |
((uint32_t(nColor64) >> 16) & 0x00ff00) | |
| 184 |
((uint32_t(nColor64) >> 8) & 0x0000ff); |
| 185 |
} |
| 186 |
|
| 187 |
default: |
| 188 |
break; |
| 189 |
} |
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
uint64_t ColorItem::rgb48() const noexcept |
| 194 |
{ |
| 195 |
switch (enState) |
| 196 |
{ |
| 197 |
case STATE_COLOR64: |
| 198 |
return nColor64; |
| 199 |
|
| 200 |
case STATE_COLOR32: |
| 201 |
{ |
| 202 |
const uint64_t c = |
| 203 |
(uint64_t(nColor32) & 0xff0000) << 16 | |
| 204 |
((nColor32 & 0x00ff00) << 8) | |
| 205 |
(nColor32 & 0x0000ff); |
| 206 |
return c | (c << 8); |
| 207 |
} |
| 208 |
|
| 209 |
default: |
| 210 |
break; |
| 211 |
} |
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
bool ColorItem::get(lsp::Color & color) const noexcept |
| 216 |
{ |
| 217 |
switch (enState) |
| 218 |
{ |
| 219 |
case STATE_COLOR64: |
| 220 |
{ |
| 221 |
const float r = ((nColor64 >> 32) & 0x00ffff) * COLOR64_TO_FLOAT; |
| 222 |
const float g = ((nColor64 >> 16) & 0x00ffff) * COLOR64_TO_FLOAT; |
| 223 |
const float b = (nColor64 & 0x00ffff) * COLOR64_TO_FLOAT; |
| 224 |
color.set_rgba(r, g, b, 0.0f); |
| 225 |
return true; |
| 226 |
} |
| 227 |
|
| 228 |
case STATE_COLOR32: |
| 229 |
{ |
| 230 |
const float r = ((nColor32 >> 16) & 0x0000ff) * COLOR32_TO_FLOAT; |
| 231 |
const float g = ((nColor32 >> 8) & 0x0000ff) * COLOR32_TO_FLOAT; |
| 232 |
const float b = (nColor32 & 0x0000ff) * COLOR32_TO_FLOAT; |
| 233 |
color.set_rgba(r, g, b, 0.0f); |
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
default: |
| 238 |
break; |
| 239 |
} |
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
bool ColorItem::get(lsp::Color * color) const noexcept |
| 244 |
{ |
| 245 |
return (color != NULL) ? get(*color) : false; |
| 246 |
} |
| 247 |
|
| 248 |
void ColorItem::set_rgb24(uint32_t value) noexcept |
| 249 |
{ |
| 250 |
if ((enState == STATE_NAME) && (sName != NULL)) |
| 251 |
{ |
| 252 |
free(sName); |
| 253 |
sName = NULL; |
| 254 |
} |
| 255 |
nColor32 = value; |
| 256 |
enState = STATE_COLOR32; |
| 257 |
} |
| 258 |
|
| 259 |
void ColorItem::set_rgb48(uint64_t value) noexcept |
| 260 |
{ |
| 261 |
if ((enState == STATE_NAME) && (sName != NULL)) |
| 262 |
{ |
| 263 |
free(sName); |
| 264 |
sName = NULL; |
| 265 |
} |
| 266 |
nColor64 = value; |
| 267 |
enState = STATE_COLOR64; |
| 268 |
} |
| 269 |
|
| 270 |
void ColorItem::swap(ColorItem & src) noexcept |
| 271 |
{ |
| 272 |
char * const tmp_n = sName; |
| 273 |
uint64_t tmp_c = nColor64; |
| 274 |
nColor64 = src.nColor64; |
| 275 |
sName = src.sName; |
| 276 |
src.nColor64 = tmp_c; |
| 277 |
src.sName = tmp_n; |
| 278 |
|
| 279 |
lsp::swap(enState, src.enState); |
| 280 |
} |
| 281 |
|
| 282 |
void ColorItem::swap(ColorItem * src) noexcept |
| 283 |
{ |
| 284 |
char * const tmp_n = sName; |
| 285 |
uint64_t tmp_c = nColor64; |
| 286 |
nColor64 = src->nColor64; |
| 287 |
sName = src->sName; |
| 288 |
src->nColor64 = tmp_c; |
| 289 |
src->sName = tmp_n; |
| 290 |
|
| 291 |
lsp::swap(enState, src->enState); |
| 292 |
} |
| 293 |
|
| 294 |
bool ColorItem::set(const ColorItem & src) |
| 295 |
{ |
| 296 |
if (&src == this) |
| 297 |
return true; |
| 298 |
|
| 299 |
// Source color item does not contain name |
| 300 |
if (src.enState != STATE_NAME) |
| 301 |
{ |
| 302 |
if (enState == STATE_NAME) |
| 303 |
free(sName); |
| 304 |
|
| 305 |
sName = NULL; |
| 306 |
nColor64 = src.nColor64; |
| 307 |
enState = src.enState; |
| 308 |
return true; |
| 309 |
} |
| 310 |
|
| 311 |
// Source color item contains name |
| 312 |
char *s = NULL; |
| 313 |
if (src.sName != NULL) |
| 314 |
{ |
| 315 |
if ((s = strdup(src.sName)) == NULL) |
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
if (enState == STATE_NAME) |
| 320 |
{ |
| 321 |
if (sName != NULL) |
| 322 |
free(sName); |
| 323 |
sName = s; |
| 324 |
} |
| 325 |
else |
| 326 |
{ |
| 327 |
nColor64 = 0; |
| 328 |
sName = s; |
| 329 |
enState = src.enState; |
| 330 |
} |
| 331 |
|
| 332 |
return true; |
| 333 |
} |
| 334 |
|
| 335 |
ColorItem & ColorItem::operator = (const ColorItem & src) |
| 336 |
{ |
| 337 |
set(src); |
| 338 |
return *this; |
| 339 |
} |
| 340 |
|
| 341 |
ColorItem & ColorItem::operator = (ColorItem && src) noexcept |
| 342 |
{ |
| 343 |
if (&src == this) |
| 344 |
return *this; |
| 345 |
|
| 346 |
if ((enState == STATE_NAME) && (sName != NULL)) |
| 347 |
free(sName); |
| 348 |
|
| 349 |
nColor64 = src.nColor64; |
| 350 |
sName = src.sName; |
| 351 |
enState = src.enState; |
| 352 |
|
| 353 |
src.nColor64 = 0; |
| 354 |
src.sName = NULL; |
| 355 |
src.enState = STATE_UNSET; |
| 356 |
|
| 357 |
return *this; |
| 358 |
} |
| 359 |
|
| 360 |
void ColorItem::reset() |
| 361 |
{ |
| 362 |
if ((enState == STATE_NAME) && (sName != NULL)) |
| 363 |
{ |
| 364 |
free(sName); |
| 365 |
sName = NULL; |
| 366 |
} |
| 367 |
nColor64 = 0; |
| 368 |
enState = STATE_UNSET; |
| 369 |
} |
| 370 |
|
| 371 |
} /* namespace xpm */ |
| 372 |
} /* namespace lsp */ |
| 373 |
|
| 374 |
|