1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 /***************************************************************************
8                           colorchart.cpp  -  description
9                              -------------------
10     begin                : Sat Sep 15 2001
11     copyright            : (C) 2001 by Franz Schmid
12     email                : Franz.Schmid@altmuehlnet.de
13  ***************************************************************************/
14 
15 /***************************************************************************
16  *                                                                         *
17  *   This program is free software; you can redistribute it and/or modify  *
18  *   it under the terms of the GNU General Public License as published by  *
19  *   the Free Software Foundation; either version 2 of the License, or     *
20  *   (at your option) any later version.                                   *
21  *                                                                         *
22  ***************************************************************************/
23 
24 #include "colorchart.h"
25 
26 #include <algorithm>
27 
28 #include <QMouseEvent>
29 #include <QPainter>
30 #include <QPaintEvent>
31 #include <QResizeEvent>
32 
33 #include "iconmanager.h"
34 #include "util_color.h"
35 #include "sccolorengine.h"
36 #include "scribuscore.h"
37 #include "scribusdoc.h"
38 
ColorChart(QWidget * parent)39 ColorChart::ColorChart(QWidget *parent) : QWidget(parent), m_doc(nullptr)
40 {
41 	setAutoFillBackground(false);
42 	drawPalette(m_currentValue);
43 }
44 
ColorChart(QWidget * parent,ScribusDoc * doc)45 ColorChart::ColorChart(QWidget *parent, ScribusDoc* doc) : QWidget(parent), m_doc(doc)
46 {
47 	setAutoFillBackground(false);
48 	drawPalette(m_currentValue);
49 }
50 
mouseMoveEvent(QMouseEvent * m)51 void ColorChart::mouseMoveEvent(QMouseEvent *m)
52 {
53 	drawMark(m->x(), m->y());
54 	if (drawMode > 0)
55 		emit ColorVal(m->x() * 256 / width() - 128, 256 - (m->y() * 256 / height()) - 128, true);
56 	else
57 		emit ColorVal(m->x() * 359 / width(), m->y() * 255 / height(), true);
58 }
59 
mousePressEvent(QMouseEvent * m)60 void ColorChart::mousePressEvent(QMouseEvent *m)
61 {
62 	drawMark(m->x(), m->y());
63 	if (drawMode > 0)
64 		emit ColorVal(m->x() * 256 / width() - 128, 256 - (m->y() * 256 / height()) - 128, true);
65 	else
66 		emit ColorVal(m->x() * 359 / width(), m->y() * 255 / height(), true);
67 }
68 
mouseReleaseEvent(QMouseEvent * m)69 void ColorChart::mouseReleaseEvent(QMouseEvent *m)
70 {
71 	drawMark(m->x(), m->y());
72 	if (drawMode > 0)
73 		emit ColorVal(m->x() * 256 / width() - 128, 256 - (m->y() * 256 / height()) - 128, true);
74 	else
75 		emit ColorVal(m->x() * 359 / width(), m->y() * 255 / height(), true);
76 }
77 
paintEvent(QPaintEvent * e)78 void ColorChart::paintEvent(QPaintEvent *e)
79 {
80 	QPainter p;
81 	QPainter p2;
82 
83 	QImage tmp(width(), height(), QImage::Format_ARGB32_Premultiplied);
84 	p2.begin(&tmp);
85 	p2.drawPixmap(0, 0, m_pixmap);
86 	if (m_drawMark)
87 	{
88 		double markX = m_markX * width();
89 		double markY = m_markY * height();
90 		p2.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
91 		p2.drawLine(markX - 5, markY - 5, markX - 1, markY - 1);
92 		p2.drawLine(markX - 5, markY + 5, markX - 1, markY + 1);
93 		p2.drawLine(markX + 2, markY + 2, markX + 6, markY + 6);
94 		p2.drawLine(markX + 2, markY - 2, markX + 6, markY - 6);
95 		m_drawMark = false;
96 	}
97 	p2.end();
98 
99 	p.begin(this);
100 	p.setClipRect(e->rect());
101 	p.drawImage(0, 0, tmp);
102 	p.end();
103 }
104 
resizeEvent(QResizeEvent * e)105 void ColorChart::resizeEvent(QResizeEvent *e)
106 {
107 	if (e->oldSize() != e->size())
108 	{
109 		if (drawMode > 0)
110 			drawLabColorMap(m_currentValue);
111 		else
112 			drawHsvColorMap(m_currentValue);
113 	}
114 	QWidget::resizeEvent(e);
115 }
116 
drawMark(int x,int y)117 void ColorChart::drawMark(int x, int y)
118 {
119 	m_markX = x / (double) width();
120 	m_markY = y / (double) height();
121 	m_drawMark = true;
122 	update();
123 }
124 
setMark(int h,int s)125 void ColorChart::setMark(int h, int s)
126 {
127 	if (drawMode > 0)
128 		drawMark((h + 128) / 256.0 * width(), (-s + 128) / 256.0 * height());
129 	else
130 		drawMark(h * width() / 359, (255 - s) * height() / 255);
131 }
132 
drawPalette(int val)133 void ColorChart::drawPalette(int val)
134 {
135 	m_currentValue = std::max(0, std::min(val, 255));
136 
137 	if (drawMode > 0)
138 		drawLabColorMap(m_currentValue);
139 	else
140 		drawHsvColorMap(m_currentValue);
141 	update();
142 }
143 
drawHsvColorMap(int val)144 void ColorChart::drawHsvColorMap(int val)
145 {
146 	int xSize = width();
147 	int ySize = height();
148 
149 	QImage image(xSize, ySize, QImage::Format_ARGB32_Premultiplied);
150 	QColor color;
151 	for (int y = 0; y < ySize; ++y)
152 	{
153 		unsigned int* p = reinterpret_cast<unsigned int*>(image.scanLine(y));
154 		for(int x = 0; x < xSize; ++x)
155 		{
156 			color.setHsv(360 * x / xSize, 256 * (ySize - 1 - y) / ySize, val);
157 			*p = color.rgb();
158 			++p;
159 		}
160 	}
161 	m_pixmap = QPixmap::fromImage(ProofImage(&image, m_doc));
162 }
163 
drawLabColorMap(int val)164 void ColorChart::drawLabColorMap(int val)
165 {
166 	int xSize = width();
167 	int ySize = height();
168 
169 	QImage image(128, 128, QImage::Format_ARGB32);
170 	bool doSoftProofing = m_doc ? m_doc->SoftProofing : false;
171 	bool doGamutCheck   = m_doc ? m_doc->Gamut : false;
172 	if (doSoftProofing && doGamutCheck)
173 	{
174 		QPainter p;
175 		QBrush b(QColor(205, 205, 205), IconManager::instance().loadPixmap("testfill.png"));
176 		p.begin(&image);
177 		p.fillRect(0, 0, image.width(), image.height(), b);
178 		p.end();
179 	}
180 	QColor color;
181 	double L = val /  2.55;
182 	for (int y = 0; y < 128; y++)
183 	{
184 		unsigned int* p = reinterpret_cast<unsigned int*>(image.scanLine(y));
185 		for (int x = 0; x < 128; x++)
186 		{
187 			double yy = 256 - (y * 2.0);
188 			if (doSoftProofing && doGamutCheck)
189 			{
190 				bool outOfG = false;
191 				color = ScColorEngine::getDisplayColorGC(ScColor(L, (x * 2.0) - 128.0, yy - 128.0), m_doc, &outOfG);
192 				if (!outOfG)
193 					*p = color.rgb();
194 			}
195 			else
196 			{
197 				color = ScColorEngine::getDisplayColor(ScColor(L, (x * 2.0) - 128.0, yy - 128.0), m_doc);
198 				*p = color.rgb();
199 			}
200 			++p;
201 		}
202 	}
203 	m_pixmap = QPixmap::fromImage(image.scaled(xSize, ySize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
204 }
205