Krita Source Code Documentation
Loading...
Searching...
No Matches
ManagedColor.cpp
Go to the documentation of this file.
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>
10#include <KoColorProfile.h>
12#include <KoChannelInfo.h>
13
14#include <QDomDocument>
15#include <QDomElement>
16
17#include <Canvas.h>
18
21
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{
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
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()) {
65 if (converter) {
66 c = converter->toQColor(d->color);
67 } else {
69 }
70 } else {
72 }
73 return c;
74}
75
76ManagedColor *ManagedColor::fromQColor(const QColor &qcolor, Canvas *canvas)
77{
78 KoColor c;
79 if (canvas && canvas->displayColorConverter() && canvas->displayColorConverter()->displayRendererInterface()) {
81 if (converter) {
82 c = converter->approximateFromRenderedQColor(qcolor);
83 } else {
85 }
86 } else {
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{
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{
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();
133 QVector<float> valuesUnsorted = components();
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
147QString ManagedColor::toXML() const
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
157void ManagedColor::fromXML(const QString &xml)
158{
159 QDomDocument doc;
160 doc.setContent(xml);
161 QDomElement e = doc.documentElement();
162 QDomElement c = e.firstChildElement("Color");
163 KoColor kc;
164 if (!c.isNull()) {
165 QString colorDepthId = c.attribute("bitdepth", Integer8BitsColorDepthID.id());
166 d->color = KoColor::fromXML(c, colorDepthId);
167 }
168
169}
170
172{
173 return KoColor::toQString(d->color);
174}
175
177{
178 return d->color;
179}
const KoID Integer8BitsColorDepthID("U8", ki18n("8-bit integer/channel"))
KisDisplayColorConverter * displayColorConverter() const
Definition Canvas.cpp:131
KoColorDisplayRendererInterface * displayRendererInterface() const
static int displayPositionToChannelIndex(int displayPosition, const QList< KoChannelInfo * > &channels)
virtual KoColor approximateFromRenderedQColor(const QColor &c) const =0
virtual QColor toQColor(const KoColor &c, bool proofToPaintColors=false) const =0
static KoColor fromXML(const QDomElement &elt, const QString &channelDepthId)
Definition KoColor.cpp:350
static QString toQString(const KoColor &color)
toQString create a user-visible string of the channel names and the channel values
Definition KoColor.cpp:655
static KoColorDisplayRendererInterface * instance()
QString id() const
Definition KoID.cpp:63
The ManagedColor class is a class to handle colors that are color managed. A managed color is a color...
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 operator==(const ManagedColor &other) const
bool setColorProfile(const QString &colorProfile)
setColorProfile set the color profile of the image to the given profile. The profile has to be regist...
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
bool setColorSpace(const QString &colorModel, const QString &colorDepth, const QString &colorProfile)
setColorSpace convert the nodes and the image to the given colorspace. The conversion is done with Pe...
QString toQString()
toQString create a user-visible string of the channel names and the channel values
void fromXML(const QString &xml)
const QScopedPointer< Private > d
~ManagedColor() override
void setComponents(const QVector< float > &values)
setComponents Set the channel/components with normalized values. For integer colorspace,...
KoColor color() const
QVector< float > componentsOrdered() const
componentsOrdered()
QString colorDepth() const
const KoColorProfile * profileByName(const QString &name) const
const KoColorSpace * colorSpace(const QString &colorModelId, const QString &colorDepthId, const KoColorProfile *profile)
static KoColorSpaceRegistry * instance()