| 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: 20 янв. 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/fmt/xpm/Color.h> |
| 23 |
#include <lsp-plug.in/stdlib/string.h> |
| 24 |
|
| 25 |
namespace lsp |
| 26 |
{ |
| 27 |
namespace xpm |
| 28 |
{ |
| 29 |
Color::~Color() |
| 30 |
{ |
| 31 |
if (sCode != NULL) |
| 32 |
{ |
| 33 |
free(sCode); |
| 34 |
sCode = NULL; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
Color::Color() noexcept |
| 39 |
{ |
| 40 |
sCode = NULL; |
| 41 |
} |
| 42 |
|
| 43 |
Color::Color(const char * code) |
| 44 |
{ |
| 45 |
sCode = (code != NULL) ? strdup(code) : NULL; |
| 46 |
} |
| 47 |
|
| 48 |
Color::Color(const char * code, size_t len) |
| 49 |
{ |
| 50 |
sCode = (code != NULL) ? strndup(code, len) : NULL;; |
| 51 |
} |
| 52 |
|
| 53 |
Color::Color(const Color & src): |
| 54 |
sMono(src.sMono), |
| 55 |
sSymbolic(src.sSymbolic), |
| 56 |
sGray4(src.sGray4), |
| 57 |
sGray(src.sGray), |
| 58 |
sColor(src.sColor) |
| 59 |
{ |
| 60 |
sCode = (src.sCode != NULL) ? strdup(src.sCode) : NULL; |
| 61 |
} |
| 62 |
|
| 63 |
Color::Color(Color && src) noexcept: |
| 64 |
sMono(lsp::move(src.sMono)), |
| 65 |
sSymbolic(lsp::move(src.sSymbolic)), |
| 66 |
sGray4(lsp::move(src.sGray4)), |
| 67 |
sGray(lsp::move(src.sGray)), |
| 68 |
sColor(lsp::move(src.sColor)) |
| 69 |
{ |
| 70 |
sCode = lsp::exchange(src.sCode, static_cast<char *>(NULL)); |
| 71 |
} |
| 72 |
|
| 73 |
void Color::clear_code() |
| 74 |
{ |
| 75 |
if (sCode != NULL) |
| 76 |
{ |
| 77 |
free(sCode); |
| 78 |
sCode = NULL; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
bool Color::set(const Color & src) |
| 83 |
{ |
| 84 |
if (&src == this) |
| 85 |
return true; |
| 86 |
|
| 87 |
Color tmp; |
| 88 |
if (!tmp.set_code(src.sCode)) |
| 89 |
return false; |
| 90 |
if (!tmp.sMono.set(src.sMono)) |
| 91 |
return false; |
| 92 |
if (!tmp.sSymbolic.set(src.sSymbolic)) |
| 93 |
return false; |
| 94 |
if (!tmp.sGray4.set(src.sGray4)) |
| 95 |
return false; |
| 96 |
if (!tmp.sGray.set(src.sGray)) |
| 97 |
return false; |
| 98 |
if (!tmp.sColor.set(src.sColor)) |
| 99 |
return false; |
| 100 |
|
| 101 |
swap(tmp); |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
bool Color::set_code(const char *code) |
| 106 |
{ |
| 107 |
if (code == NULL) |
| 108 |
{ |
| 109 |
clear_code(); |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
char *c = strdup(code); |
| 114 |
if (c == NULL) |
| 115 |
return false; |
| 116 |
|
| 117 |
if (sCode != NULL) |
| 118 |
free(sCode); |
| 119 |
sCode = c; |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
bool Color::set_code(const char *code, size_t len) |
| 124 |
{ |
| 125 |
if (code == NULL) |
| 126 |
{ |
| 127 |
clear_code(); |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
char *c = strmemdup(code, len); |
| 132 |
if (c == NULL) |
| 133 |
return false; |
| 134 |
|
| 135 |
if (sCode != NULL) |
| 136 |
free(sCode); |
| 137 |
sCode = c; |
| 138 |
|
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
bool Color::has_code(const char *code) const noexcept |
| 143 |
{ |
| 144 |
if (code == sCode) // Handle if NULL has been passed |
| 145 |
return true; |
| 146 |
if (sCode == NULL) |
| 147 |
return false; |
| 148 |
|
| 149 |
return strcmp(sCode, code) == 0; |
| 150 |
} |
| 151 |
|
| 152 |
Color & Color::operator = (const Color & src) |
| 153 |
{ |
| 154 |
set(src); |
| 155 |
return *this; |
| 156 |
} |
| 157 |
|
| 158 |
Color & Color::operator = (Color && src) noexcept |
| 159 |
{ |
| 160 |
if (&src == this) |
| 161 |
return *this; |
| 162 |
|
| 163 |
if (sCode != NULL) |
| 164 |
free(sCode); |
| 165 |
|
| 166 |
sCode = lsp::exchange(src.sCode, static_cast<char *>(NULL)); |
| 167 |
sMono = lsp::move(src.sMono); |
| 168 |
sSymbolic = lsp::move(src.sSymbolic); |
| 169 |
sGray4 = lsp::move(src.sGray4); |
| 170 |
sGray = lsp::move(src.sGray); |
| 171 |
sColor = lsp::move(src.sColor); |
| 172 |
|
| 173 |
return *this; |
| 174 |
} |
| 175 |
|
| 176 |
void Color::swap(Color & src) noexcept |
| 177 |
{ |
| 178 |
lsp::swap(sCode, src.sCode); |
| 179 |
sMono.swap(src.sMono); |
| 180 |
sSymbolic.swap(src.sSymbolic); |
| 181 |
sGray4.swap(src.sGray4); |
| 182 |
sGray.swap(src.sGray); |
| 183 |
sColor.swap(src.sColor); |
| 184 |
} |
| 185 |
|
| 186 |
void Color::swap(Color * src) noexcept |
| 187 |
{ |
| 188 |
lsp::swap(sCode, src->sCode); |
| 189 |
sMono.swap(src->sMono); |
| 190 |
sSymbolic.swap(src->sSymbolic); |
| 191 |
sGray4.swap(src->sGray4); |
| 192 |
sGray.swap(src->sGray); |
| 193 |
sColor.swap(src->sColor); |
| 194 |
} |
| 195 |
|
| 196 |
} /* namespace xpm */ |
| 197 |
} /* namespace lsp */ |
| 198 |
|
| 199 |
|