Krita

ManagedColor.cpp
1 /*
2  * SPDX-FileCopyrightText: 2017 Boudewijn Rempt <[email protected]>
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 
22 struct ManagedColor::Private {
23  KoColor color;
24 };
25 
27  : QObject(parent)
28  , d(new Private())
29 {
30  // Default black rgb color
31 }
32 
33 ManagedColor::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 
44 ManagedColor::ManagedColor(KoColor color, QObject *parent)
45  : QObject(parent)
46  , d(new Private())
47 {
48  d->color = color;
49 }
50 
51 ManagedColor::~ManagedColor()
52 {
53 }
54 
55 bool 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 
107 bool 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 
115 bool 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  QVector<float> valuesUnsorted = components();
133  QVector<float> values(d->color.colorSpace()->channelCount());
134  for (int i=0; i<values.size();i++) {
135  int location = KoChannelInfo::displayPositionToChannelIndex(i, d->color.colorSpace()->channels());
136  values[location] = valuesUnsorted[i];
137  }
138  return values;
139 }
140 
142 {
143  d->color.colorSpace()->fromNormalisedChannelsValue(d->color.data(), values);
144 }
145 
147 {
148  QDomDocument doc;
149  QDomElement root = doc.createElement("Color");
150  root.setAttribute("bitdepth", colorDepth());
151  doc.appendChild(root);
152  d->color.toXML(doc, root);
153  return doc.toString();
154 }
155 
157 {
158  QDomDocument doc;
159  doc.setContent(xml);
160  QDomElement e = doc.documentElement();
161  QDomElement c = e.firstChildElement("Color");
162  KoColor kc;
163  if (!c.isNull()) {
164  QString colorDepthId = c.attribute("bitdepth", Integer8BitsColorDepthID.id());
165  d->color = KoColor::fromXML(c, colorDepthId);
166  }
167 
168 }
169 
171 {
172  return KoColor::toQString(d->color);
173 }
174 
175 KoColor ManagedColor::color() const
176 {
177  return d->color;
178 }
void fromXML(const QString &xml)
Unserialize a color following Create's swatch color specification available at https://web....
QString colorDepth() const
colorDepth A string describing the color depth of the image:
QString toString(int indent) const const
void setComponents(const QVector< float > &values)
setComponents Set the channel/components with normalized values.
bool setColorSpace(const QString &colorModel, const QString &colorDepth, const QString &colorProfile)
setColorSpace convert the nodes and the image to the given colorspace.
QString colorModel() const
colorModel retrieve the current color model of this document:
bool isNull() const const
QVector< float > components() const
components
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
ManagedColor(QObject *parent=0)
ManagedColor Create a ManagedColor that is black and transparent.
QDomElement createElement(const QString &tagName)
void setAttribute(const QString &name, const QString &value)
T * data() const const
QString toQString()
toQString create a user-visible string of the channel names and the channel values
bool setColorProfile(const QString &colorProfile)
setColorProfile set the color profile of the image to the given profile.
QString colorProfile() const
The ManagedColor class is a class to handle colors that are color managed.
Definition: ManagedColor.h:45
QColor colorForCanvas(Canvas *canvas) const
colorForCanvas
QDomElement documentElement() const const
QVector< float > componentsOrdered() const
componentsOrdered()
QDomElement firstChildElement(const QString &tagName) const const
QString toXML() const
Serialize this color following Create's swatch color specification available at https://web....
QDomNode appendChild(const QDomNode &newChild)
Canvas wraps the canvas inside a view on an image/document.
Definition: Canvas.h:22
QString attribute(const QString &name, const QString &defValue) const const
static ManagedColor * fromQColor(const QColor &qcolor, Canvas *canvas=0)
fromQColor is the (approximate) reverse of colorForCanvas()
QVector< V > values(const QMultiHash< K, V > &c)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Dec 4 2022 04:12:55 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.