Krita

ManagedColor.cpp
1/*
2 * SPDX-FileCopyrightText: 2017 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "ManagedColor.h"
8#include <KoColor.h>
9#include <KoColorSpaceRegistry.h>
10#include <KoColorProfile.h>
11#include <KoColorModelStandardIds.h>
12#include <KoChannelInfo.h>
13
14#include <QDomDocument>
15#include <QDomElement>
16
17#include <Canvas.h>
18
19#include <kis_display_color_converter.h>
20#include <KoColorDisplayRendererInterface.h>
21
22struct ManagedColor::Private {
23 KoColor color;
24};
25
27 : QObject(parent)
28 , d(new Private())
29{
30 // Default black rgb color
31}
32
33ManagedColor::ManagedColor(const QString &colorModel, const QString &colorDepth, const QString &colorProfile, QObject *parent)
34 : QObject(parent)
35 , d(new Private())
36{
37 const KoColorSpace *colorSpace = KoColorSpaceRegistry::instance()->colorSpace(colorModel, colorDepth, colorProfile);
38 if (colorSpace) {
39 d->color = KoColor(colorSpace);
40 }
41}
42
43
44ManagedColor::ManagedColor(KoColor color, QObject *parent)
45 : QObject(parent)
46 , d(new Private())
47{
48 d->color = color;
49}
50
51ManagedColor::~ManagedColor()
52{
53}
54
55bool ManagedColor::operator==(const ManagedColor &other) const
56{
57 return d->color == other.d->color;
58}
59
61{
62 QColor c = QColor(0,0,0);
63 if (canvas && canvas->displayColorConverter() && canvas->displayColorConverter()->displayRendererInterface()) {
64 KoColorDisplayRendererInterface *converter = canvas->displayColorConverter()->displayRendererInterface();
65 if (converter) {
66 c = converter->toQColor(d->color);
67 } else {
68 c = KoDumbColorDisplayRenderer::instance()->toQColor(d->color);
69 }
70 } else {
71 c = KoDumbColorDisplayRenderer::instance()->toQColor(d->color);
72 }
73 return c;
74}
75
77{
78 KoColor c;
79 if (canvas && canvas->displayColorConverter() && canvas->displayColorConverter()->displayRendererInterface()) {
80 KoColorDisplayRendererInterface *converter = canvas->displayColorConverter()->displayRendererInterface();
81 if (converter) {
82 c = converter->approximateFromRenderedQColor(qcolor);
83 } else {
84 c = KoDumbColorDisplayRenderer::instance()->approximateFromRenderedQColor(qcolor);
85 }
86 } else {
87 c = KoDumbColorDisplayRenderer::instance()->approximateFromRenderedQColor(qcolor);
88 }
89 return new ManagedColor(c);
90}
91
93{
94 return d->color.colorSpace()->colorDepthId().id();
95}
96
98{
99 return d->color.colorSpace()->colorModelId().id();
100}
101
103{
104 return d->color.colorSpace()->profile()->name();
105}
106
107bool ManagedColor::setColorProfile(const QString &colorProfile)
108{
109 const KoColorProfile *profile = KoColorSpaceRegistry::instance()->profileByName(colorProfile);
110 if (!profile) return false;
111 d->color.setProfile(profile);
112 return true;
113}
114
115bool ManagedColor::setColorSpace(const QString &colorModel, const QString &colorDepth, const QString &colorProfile)
116{
117 const KoColorSpace *colorSpace = KoColorSpaceRegistry::instance()->colorSpace(colorModel, colorDepth, colorProfile);
118 if (!colorSpace) return false;
119 d->color.convertTo(colorSpace);
120 return true;
121}
122
124{
125 QVector<float> values(d->color.colorSpace()->channelCount());
126 d->color.colorSpace()->normalisedChannelsValue(d->color.data(), values);
127 return values;
128}
129
131{
132 const QList<KoChannelInfo *> channelInfo = d->color.colorSpace()->channels();
134 QVector<float> values(channelInfo.size());
135 for (int i=0; i<values.size();i++) {
136 int location = KoChannelInfo::displayPositionToChannelIndex(i, channelInfo);
137 values[location] = valuesUnsorted[i];
138 }
139 return values;
140}
141
143{
144 d->color.colorSpace()->fromNormalisedChannelsValue(d->color.data(), values);
145}
146
148{
149 QDomDocument doc;
150 QDomElement root = doc.createElement("Color");
151 root.setAttribute("bitdepth", colorDepth());
152 doc.appendChild(root);
153 d->color.toXML(doc, root);
154 return doc.toString();
155}
156
158{
159 QDomDocument doc;
160 doc.setContent(xml);
162 QDomElement c = e.firstChildElement("Color");
163 KoColor kc;
164 if (!c.isNull()) {
166 d->color = KoColor::fromXML(c, colorDepthId);
167 }
168
169}
170
172{
173 return KoColor::toQString(d->color);
174}
175
176KoColor ManagedColor::color() const
177{
178 return d->color;
179}
Canvas wraps the canvas inside a view on an image/document.
Definition Canvas.h:23
The ManagedColor class is a class to handle colors that are color managed.
QString colorModel() const
colorModel retrieve the current color model of this document:
ManagedColor(QObject *parent=0)
ManagedColor Create a ManagedColor that is black and transparent.
bool setColorProfile(const QString &colorProfile)
setColorProfile set the color profile of the image to the given profile.
QString colorProfile() const
QVector< float > components() const
components
QColor colorForCanvas(Canvas *canvas) const
colorForCanvas
static ManagedColor * fromQColor(const QColor &qcolor, Canvas *canvas=0)
fromQColor is the (approximate) reverse of colorForCanvas()
QString toXML() const
Serialize this color following Create's swatch color specification available at https://web....
bool setColorSpace(const QString &colorModel, const QString &colorDepth, const QString &colorProfile)
setColorSpace convert the nodes and the image to the given colorspace.
QString toQString()
toQString create a user-visible string of the channel names and the channel values
void fromXML(const QString &xml)
Unserialize a color following Create's swatch color specification available at https://web....
void setComponents(const QVector< float > &values)
setComponents Set the channel/components with normalized values.
QVector< float > componentsOrdered() const
componentsOrdered()
QString colorDepth() const
colorDepth A string describing the color depth of the image:
QDomElement createElement(const QString &tagName)
QDomElement documentElement() const const
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
QString toString(int indent) const const
QString attribute(const QString &name, const QString &defValue) const const
void setAttribute(const QString &name, const QString &value)
QDomNode appendChild(const QDomNode &newChild)
QDomElement firstChildElement(const QString &tagName) const const
bool isNull() const const
T qobject_cast(QObject *object)
int size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:47:29 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.