Krita Source Code Documentation
Loading...
Searching...
No Matches
KisVisualColorModel.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Wolthera van Hovell tot Westerflier <griffinvalley@gmail.com>
3 * SPDX-FileCopyrightText: 2022 Mathias Wein <lynx.mw+kde@gmail.com>
4 *
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 */
7
9
10#include <QVector>
11#include <QVector3D>
12#include <QVector4D>
13#include <QList>
14#include <QtMath>
15
16#include <KSharedConfig>
17#include <KConfigGroup>
18
19#include "KoColorConversions.h"
21#include "KoColorProfile.h"
22#include "KoChannelInfo.h"
26#include "kis_debug.h"
27
29{
32 bool exposureSupported {false};
33 bool isRGBA {false};
34 bool isLABlike {false};
35 bool isLinear {false};
36 bool applyGamma {false};
37 bool allowUpdates {true};
38 int logicalToMemoryPosition[4]; // map logical channel index to storage index for display
40 qreal gamma {2.2};
41 qreal lumaRGB[3] {0.2126, 0.7152, 0.0722};
42 QVector4D channelValues;
47};
48
50 : QObject(0)
51 , m_d(new Private)
52{
53 KConfigGroup cfg = KSharedConfig::openConfig()->group("advancedColorSelector");
54 m_d->acs_config = KisColorSelectorConfiguration::fromString(cfg.readEntry("colorSelectorConfiguration", KisColorSelectorConfiguration().toString()));
55
56}
57
61
63{
64 if (!m_d->allowUpdates) {
65 return;
66 }
67
68 if (!m_d->currentCS) {
69 m_d->currentcolor = c;
71 }
72 else {
73 m_d->currentcolor = c.convertedTo(m_d->currentCS);
74 m_d->channelValues = convertKoColorToChannelValues(m_d->currentcolor);
76 }
77}
78
80{
81 if (!m_d->currentCS || *m_d->currentCS != *cs) {
82 const KoColorSpace *csNew = cs;
83
84 // PQ color space is not very suitable for color picking, substitute with linear one
88
92 }
93
94
95 // TODO: split off non-config related initializations
96 loadColorSpace(csNew);
97 m_d->currentcolor = KoColor(csNew);
98 m_d->channelValues = convertKoColorToChannelValues(m_d->currentcolor);
99 Q_EMIT sigColorSpaceChanged();
100 }
101}
102
103void KisVisualColorModel::slotSetChannelValues(const QVector4D &values)
104{
105 if (!m_d->allowUpdates) {
106 return;
107 }
108
109 quint32 changeFlags = 0;
110 QVector4D newValues(0, 0, 0, 0);
111 for (int i = 0; i < m_d->colorChannelCount; i++) {
112 newValues[i] = values[i];
113 changeFlags |= quint32(values[i] != m_d->channelValues[i]) << i;
114 }
115 if (changeFlags != 0) {
116 m_d->allowUpdates = false;
117 m_d->channelValues = newValues;
118 m_d->currentcolor = convertChannelValuesToKoColor(newValues);
119 Q_EMIT sigChannelValuesChanged(m_d->channelValues, changeFlags);
120 Q_EMIT sigNewColor(m_d->currentcolor);
121 m_d->allowUpdates = true;
122 }
123}
124
126{
127 return m_d->currentcolor;
128}
129
131{
132 return m_d->channelValues;
133}
134
136{
137 return m_d->colorChannelCount;
138}
139
144
146{
147 return m_d->channelMaxValues;
148}
149
150void KisVisualColorModel::setMaxChannelValues(const QVector4D &maxValues)
151{
152 if (maxValues == m_d->channelMaxValues) {
153 return;
154 }
155 m_d->channelMaxValues = maxValues;
156 if (m_d->exposureSupported) {
157 // need to re-scale our normalized channel values on exposure changes:
158 m_d->channelValues = convertKoColorToChannelValues(m_d->currentcolor);
160 }
161}
162
164{
165 m_d->channelMaxValues = other.m_d->channelMaxValues;
166 setRGBColorModel(other.m_d->modelRGB);
167 if (other.colorSpace()) {
170 }
171}
172
174{
175 if (model == m_d->modelRGB) {
176 return;
177 }
178 if (model >= ColorModel::HSV && model <= ColorModel::HSY) {
179 m_d->modelRGB = model;
180 if (m_d->isRGBA) {
181 m_d->model = model;
182 m_d->applyGamma = (m_d->isLinear && m_d->modelRGB != ColorModel::HSY);
183 Q_EMIT sigColorModelChanged();
184 m_d->channelValues = convertKoColorToChannelValues(m_d->currentcolor);
186 }
187 }
188}
189
191{
192 return m_d->currentCS;
193}
194
196{
197 return (m_d->model >= ColorModel::HSV && m_d->model <= ColorModel::HSY);
198}
199
201{
202 return (m_d->exposureSupported);
203}
204
206{
207 KoColor c(m_d->currentCS);
208 QVector4D baseValues(values);
209 QVector<float> channelVec(c.colorSpace()->channelCount());
210 channelVec.fill(1.0);
211
212 if (m_d->model != ColorModel::Channel && m_d->isRGBA) {
213
214 if (m_d->model == ColorModel::HSV) {
215 HSVToRGB(values.x()*360, values.y(), values.z(), &baseValues[0], &baseValues[1], &baseValues[2]);
216 }
217 else if (m_d->model == ColorModel::HSL) {
218 HSLToRGB(values.x()*360, values.y(), values.z(), &baseValues[0], &baseValues[1], &baseValues[2]);
219 }
220 else if (m_d->model == ColorModel::HSI) {
221 // why suddenly qreal?
222 qreal temp[3];
223 HSIToRGB(values.x(), values.y(), values.z(), &temp[0], &temp[1], &temp[2]);
224 baseValues.setX(temp[0]);
225 baseValues.setY(temp[1]);
226 baseValues.setZ(temp[2]);
227 }
228 else /*if (m_d->model == ColorModel::HSY)*/ {
229 qreal temp[3];
230 qreal Y = pow(values.z(), m_d->gamma);
231 HSYToRGB(values.x(), values.y(), Y, &temp[0], &temp[1], &temp[2],
232 m_d->lumaRGB[0], m_d->lumaRGB[1], m_d->lumaRGB[2]);
233 baseValues.setX(temp[0]);
234 baseValues.setY(temp[1]);
235 baseValues.setZ(temp[2]);
236 if (!m_d->isLinear) {
237 // Note: not all profiles define a TRC necessary for (de-)linearization,
238 // substituting with a linear profiles would be better
239 QVector<qreal> tempVec({baseValues[0], baseValues[1], baseValues[2]});
240 if (m_d->exposureSupported) {
241 m_d->currentCS->profile()->delinearizeFloatValue(tempVec);
242 }
243 else {
244 m_d->currentCS->profile()->delinearizeFloatValueFast(tempVec);
245 }
246 baseValues = QVector4D(tempVec[0], tempVec[1], tempVec[2], 0);
247 }
248 }
249 if (m_d->applyGamma) {
250 for (int i=0; i<3; i++) {
251 baseValues[i] = pow(baseValues[i], 2.2);
252 }
253 }
254 } else if (m_d->model != ColorModel::Channel && m_d->isLABlike) {
255 QVector<qreal> tempVec({baseValues[0], baseValues[1], baseValues[2]});
256 LCHToLab(values.z(), values.y(), values.x(), &tempVec[0], &tempVec[1], &tempVec[2]);
257 baseValues = QVector4D(tempVec[0], tempVec[1], tempVec[2], 0);
258 }
259
260 if (m_d->exposureSupported) {
261 for (int i = 0; i < m_d->colorChannelCount; i++) {
262 baseValues[i] *= m_d->channelMaxValues[i];
263 }
264 }
265
266 for (int i=0; i<m_d->colorChannelCount; i++) {
267 channelVec[m_d->logicalToMemoryPosition[i]] = baseValues[i];
268 }
269
270 c.colorSpace()->fromNormalisedChannelsValue(c.data(), channelVec);
271
272 return c;
273
274}
275
277{
278 if (c.colorSpace() != m_d->currentCS) {
279 c.convertTo(m_d->currentCS);
280 }
281 QVector<float> channelVec (c.colorSpace()->channelCount());
282 channelVec.fill(1.0);
283 m_d->currentCS->normalisedChannelsValue(c.data(), channelVec);
284 QVector4D channelValuesDisplay(0, 0, 0, 0), coordinates(0, 0, 0, 0);
285
286 for (int i =0; i<m_d->colorChannelCount; i++) {
287 channelValuesDisplay[i] = channelVec[m_d->logicalToMemoryPosition[i]];
288 }
289
290 if (m_d->exposureSupported) {
291 for (int i = 0; i < m_d->colorChannelCount; i++) {
292 channelValuesDisplay[i] /= m_d->channelMaxValues[i];
293 }
294 }
295 if (m_d->model != ColorModel::Channel && m_d->isRGBA) {
296 if (m_d->applyGamma) {
297 for (int i=0; i<3; i++) {
298 channelValuesDisplay[i] = pow(channelValuesDisplay[i], 1/2.2);
299 }
300 }
301 if (m_d->model == ColorModel::HSV) {
302 QVector3D hsv;
303 RGBToHSV(channelValuesDisplay[0], channelValuesDisplay[1], channelValuesDisplay[2], &hsv[0], &hsv[1], &hsv[2]);
304 hsv[0] /= 360;
305 coordinates = QVector4D(hsv, 0.f);
306 } else if (m_d->model == ColorModel::HSL) {
307 QVector3D hsl;
308 RGBToHSL(channelValuesDisplay[0], channelValuesDisplay[1], channelValuesDisplay[2], &hsl[0], &hsl[1], &hsl[2]);
309 hsl[0] /= 360;
310 coordinates = QVector4D(hsl, 0.f);
311 } else if (m_d->model == ColorModel::HSI) {
312 qreal hsi[3];
313 RGBToHSI(channelValuesDisplay[0], channelValuesDisplay[1], channelValuesDisplay[2], &hsi[0], &hsi[1], &hsi[2]);
314 coordinates = QVector4D(hsi[0], hsi[1], hsi[2], 0.f);
315 } else if (m_d->model == ColorModel::HSY) {
316 if (!m_d->isLinear) {
317 // Note: not all profiles define a TRC necessary for (de-)linearization,
318 // substituting with a linear profiles would be better
319 QVector<qreal> temp({channelValuesDisplay[0], channelValuesDisplay[1], channelValuesDisplay[2]});
320 m_d->currentCS->profile()->linearizeFloatValue(temp);
321 channelValuesDisplay = QVector4D(temp[0], temp[1], temp[2], 0);
322 }
323 qreal hsy[3];
324 RGBToHSY(channelValuesDisplay[0], channelValuesDisplay[1], channelValuesDisplay[2], &hsy[0], &hsy[1], &hsy[2],
325 m_d->lumaRGB[0], m_d->lumaRGB[1], m_d->lumaRGB[2]);
326 hsy[2] = pow(hsy[2], 1/m_d->gamma);
327 coordinates = QVector4D(hsy[0], hsy[1], hsy[2], 0.f);
328 }
329 // if we couldn't determine a hue, keep last value
330 if (coordinates[0] < 0) {
331 coordinates[0] = m_d->channelValues[0];
332 }
333 for (int i=0; i<3; i++) {
334 coordinates[i] = qBound(0.f, coordinates[i], 1.f);
335 }
336 } else if (m_d->model != ColorModel::Channel && m_d->isLABlike) {
337 qreal lch[3];
338 LabToLCH(channelValuesDisplay[0], channelValuesDisplay[1], channelValuesDisplay[2], &lch[0], &lch[1], &lch[2]);
339 coordinates = QVector4D(lch[2], lch[1], lch[0], 0.f);
340
341 if (coordinates[0] < 0) {
342 coordinates[0] = m_d->channelValues[0];
343 }
344 for (int i=0; i<3; i++) {
345 coordinates[i] = qBound(0.f, coordinates[i], 1.f);
346 }
347 } else {
348 for (int i=0; i<4; i++) {
349 coordinates[i] = qBound(0.f, channelValuesDisplay[i], 1.f);
350 }
351 }
352 return coordinates;
353}
354
356{
357 KConfigGroup cfg = KSharedConfig::openConfig()->group("advancedColorSelector");
358 m_d->acs_config = KisColorSelectorConfiguration::fromString(cfg.readEntry("colorSelectorConfiguration", KisColorSelectorConfiguration().toString()));
359
360 m_d->gamma = cfg.readEntry("gamma", 2.2);
361
363
364 switch(m_d->acs_config.mainTypeParameter) {
365
370 RGB_model = KisVisualColorModel::HSV;
371 break;
372
376 RGB_model = KisVisualColorModel::HSL;
377 break;
378
382 RGB_model = KisVisualColorModel::HSI;
383 break;
384
388 RGB_model = KisVisualColorModel::HSY;
389 break;
390
391 default:
393 }
394 if (m_d->acs_config.mainType == KisColorSelectorConfiguration::Triangle) {
395 //Triangle only really works in HSV mode.
396 RGB_model = KisVisualColorModel::HSV;
397 }
398
399 setRGBColorModel(RGB_model);
400}
401
403{
404 const QList<KoChannelInfo *> channelList = cs->channels();
405 int cCount = 0;
406
407 for (int i = 0; i < channelList.size(); i++) {
408 const KoChannelInfo *channel = channelList.at(i);
409 if (channel->channelType() != KoChannelInfo::ALPHA) {
410 quint32 logical = channel->displayPosition();
411 if (logical > cs->alphaPos()) {
412 --logical;
413 }
414 m_d->logicalToMemoryPosition[logical] = i;
415 m_d->channelMaxValues[logical] = channel->getUIMax();
416 ++cCount;
417 }
418 }
419 KIS_ASSERT_X(cCount < 5, "", "unsupported channel count!");
420
421 m_d->colorChannelCount = cCount;
422
427 && cs->colorModelId() != CMYKAColorModelID) {
428 m_d->exposureSupported = true;
429 } else {
430 m_d->exposureSupported = false;
431 }
432 m_d->isRGBA = (cs->colorModelId() == RGBAColorModelID);
433 m_d->isLABlike = (cs->colorModelId() == LABAColorModelID || cs->colorModelId() == YCbCrAColorModelID);
434
435 const KoColorProfile *profile = cs->profile();
436 m_d->isLinear = (profile && profile->isLinear());
437
438 if (m_d->isRGBA) {
439 m_d->applyGamma = (m_d->isLinear && m_d->modelRGB != ColorModel::HSY);
440 // Note: only profiles that define colorants will give precise luma coefficients.
441 // Maybe using the explicitly set values of the Advanced Color Selector is better?
442 QVector <qreal> luma = cs->lumaCoefficients();
443 memcpy(m_d->lumaRGB, luma.constData(), 3*sizeof(qreal));
444 m_d->model = m_d->modelRGB;
445 } else if (m_d->isLABlike) {
446 m_d->model = m_d->modelRGB;
447 } else {
449 }
450
451 const KoColorSpace *oldCS = m_d->currentCS;
452 m_d->currentCS = cs;
453
454 if (!oldCS || (oldCS->colorModelId() != cs->colorModelId())) {
455 Q_EMIT sigColorModelChanged();
456 }
457}
458
460{
461 bool updatesAllowed = m_d->allowUpdates;
462 m_d->allowUpdates = false;
463 Q_EMIT sigChannelValuesChanged(m_d->channelValues, (1u << m_d->colorChannelCount) - 1);
464 m_d->allowUpdates = updatesAllowed;
465}
void LabToLCH(const qreal l, const qreal a, const qreal b, qreal *L, qreal *C, qreal *H)
void HSYToRGB(const qreal h, const qreal s, const qreal y, qreal *red, qreal *green, qreal *blue, qreal R, qreal G, qreal B)
void RGBToHSV(float r, float g, float b, float *h, float *s, float *v)
void RGBToHSI(qreal r, qreal g, qreal b, qreal *h, qreal *s, qreal *i)
void LCHToLab(const qreal L, const qreal C, const qreal H, qreal *l, qreal *a, qreal *b)
void HSIToRGB(const qreal h, const qreal s, const qreal i, qreal *red, qreal *green, qreal *blue)
void RGBToHSL(float r, float g, float b, float *h, float *s, float *l)
void RGBToHSY(const qreal r, const qreal g, const qreal b, qreal *h, qreal *s, qreal *y, qreal R, qreal G, qreal B)
void HSVToRGB(float h, float s, float v, float *r, float *g, float *b)
void HSLToRGB(float h, float sl, float l, float *r, float *g, float *b)
const KoID Float32BitsColorDepthID("F32", ki18n("32-bit float/channel"))
const KoID YCbCrAColorModelID("YCbCrA", ki18n("YCbCr/Alpha"))
const KoID Float64BitsColorDepthID("F64", ki18n("64-bit float/channel"))
const KoID Float16BitsColorDepthID("F16", ki18n("16-bit float/channel"))
const KoID CMYKAColorModelID("CMYKA", ki18n("CMYK/Alpha"))
const KoID LABAColorModelID("LABA", ki18n("L*a*b*/Alpha"))
const KoID RGBAColorModelID("RGBA", ki18n("RGB/Alpha"))
static KisColorSelectorConfiguration fromString(QString string)
The KisVisualColorModel class allows manipulating a KoColor using various color models.
void setMaxChannelValues(const QVector4D &maxValues)
void sigColorModelChanged()
sigColorModelChanged is emitted whenever the color model changes.
const KoColorSpace * colorSpace() const
void copyState(const KisVisualColorModel &other)
Copy the internal state of another KisVisualColorModel.
void slotSetColorSpace(const KoColorSpace *cs)
void slotLoadACSConfig()
slotLoadACSConfig loads supported settings from Advanced Color Selector
QVector4D convertKoColorToChannelValues(KoColor c) const
QVector4D channelValues() const
QVector4D maxChannelValues() const
void loadColorSpace(const KoColorSpace *cs)
KoColor convertChannelValuesToKoColor(const QVector4D &values) const
void slotSetChannelValues(const QVector4D &values)
void setRGBColorModel(ColorModel model)
Set the HSX color model used for RGB color spaces.
void sigNewColor(const KoColor &c)
const QScopedPointer< Private > m_d
ColorModel colorModel() const
void slotSetColor(const KoColor &c)
void sigChannelValuesChanged(const QVector4D &values, quint32 channelFlags)
void sigColorSpaceChanged()
sigColorSpaceChanged notifies that the color space from which the channel values are derived changed,...
@ ALPHA
The channel represents the opacity of a pixel.
enumChannelType channelType() const
qint32 displayPosition() const
double getUIMax(void) const
virtual quint32 alphaPos() const =0
QList< KoChannelInfo * > channels
virtual KoID colorModelId() const =0
QVector< qreal > lumaCoefficients
virtual quint32 channelCount() const =0
virtual KoID colorDepthId() const =0
virtual void fromNormalisedChannelsValue(quint8 *pixel, const QVector< float > &values) const =0
virtual const KoColorProfile * profile() const =0
void convertTo(const KoColorSpace *cs, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags)
Definition KoColor.cpp:136
KoColor convertedTo(const KoColorSpace *cs, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags) const
Definition KoColor.cpp:163
quint8 * data()
Definition KoColor.h:144
const KoColorSpace * colorSpace() const
return the current colorSpace
Definition KoColor.h:82
QString id() const
Definition KoID.cpp:63
#define KIS_ASSERT_X(cond, where, what)
Definition kis_assert.h:40
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
KisColorSelectorConfiguration acs_config
virtual QByteArray uniqueId() const =0
virtual bool isLinear() const =0
static KoColorSpaceRegistry * instance()
const KoColorProfile * p2020PQProfile() const
const KoColorProfile * p2020G10Profile() const