r24407 by jghali - Code improvements for ColorChart

scribus-commit scribus-commit at lists.scribus.net
Sat Jan 16 21:42:34 UTC 2021


Author: jghali
Date: Sat Jan 16 21:42:34 2021
New Revision: 24407

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=24407
Log:
Code improvements for ColorChart

Modified:
    trunk/Scribus/scribus/ui/colorchart.cpp
    trunk/Scribus/scribus/ui/colorchart.h

Modified: trunk/Scribus/scribus/ui/colorchart.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=24407&path=/trunk/Scribus/scribus/ui/colorchart.cpp
==============================================================================
--- trunk/Scribus/scribus/ui/colorchart.cpp	(original)
+++ trunk/Scribus/scribus/ui/colorchart.cpp	Sat Jan 16 21:42:34 2021
@@ -23,7 +23,10 @@
 
 #include "colorchart.h"
 
+#include <QMouseEvent>
 #include <QPainter>
+#include <QPaintEvent>
+#include <QResizeEvent>
 
 #include "iconmanager.h"
 #include "util_color.h"
@@ -34,13 +37,13 @@
 ColorChart::ColorChart(QWidget *parent) : QWidget(parent), m_doc(nullptr)
 {
 	setAutoFillBackground(false);
-	drawPalette(255);
+	drawPalette(m_currentValue);
 }
 
 ColorChart::ColorChart(QWidget *parent, ScribusDoc* doc) : QWidget(parent), m_doc(doc)
 {
 	setAutoFillBackground(false);
-	drawPalette(255);
+	drawPalette(m_currentValue);
 }
 
 void ColorChart::mouseMoveEvent(QMouseEvent *m)
@@ -74,9 +77,8 @@
 {
 	QPainter p;
 	QPainter p2;
-	p.begin(this);
-	p.setClipRect(e->rect());
-	QImage tmp = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
+
+	QImage tmp(width(), height(), QImage::Format_ARGB32_Premultiplied);
 	p2.begin(&tmp);
 	p2.drawPixmap(0, 0, m_pixmap);
 	if (m_drawMark)
@@ -86,11 +88,26 @@
 		p2.drawLine(m_markX - 5, m_markY + 5, m_markX - 1, m_markY + 1);
 		p2.drawLine(m_markX + 2, m_markY + 2, m_markX + 6, m_markY + 6);
 		p2.drawLine(m_markX + 2, m_markY - 2, m_markX + 6, m_markY - 6);
+		m_drawMark = false;
 	}
 	p2.end();
+
+	p.begin(this);
+	p.setClipRect(e->rect());
 	p.drawImage(0, 0, tmp);
 	p.end();
-	m_drawMark = false;
+}
+
+void ColorChart::resizeEvent(QResizeEvent *e)
+{
+	if (e->oldSize() != e->size())
+	{
+		if (drawMode > 0)
+			drawLabColorMap(m_currentValue);
+		else
+			drawHsvColorMap(m_currentValue);
+	}
+	QWidget::resizeEvent(e);
 }
 
 void ColorChart::drawMark(int x, int y)
@@ -111,61 +128,73 @@
 
 void ColorChart::drawPalette(int val)
 {
+	m_currentValue = std::max(0, std::min(val, 255));
+
+	if (drawMode > 0)
+		drawLabColorMap(m_currentValue);
+	else
+		drawHsvColorMap(m_currentValue);
+	update();
+}
+
+void ColorChart::drawHsvColorMap(int val)
+{
 	int xSize = width();
 	int ySize = height();
-	if (drawMode > 0)
-	{
-		QImage image(128, 128, QImage::Format_ARGB32);
-		bool doSoftProofing = m_doc ? m_doc->SoftProofing : false;
-		bool doGamutCheck   = m_doc ? m_doc->Gamut : false;
-		if (doSoftProofing && doGamutCheck)
+
+	QImage image(xSize, ySize, QImage::Format_ARGB32_Premultiplied);
+	QColor color;
+	for (int y = 0; y < ySize; ++y)
+	{
+		unsigned int* p = reinterpret_cast<unsigned int*>(image.scanLine(y));
+		for(int x = 0; x < xSize; ++x)
 		{
-			QPainter p;
-			QBrush b(QColor(205, 205, 205), IconManager::instance().loadPixmap("testfill.png"));
-			p.begin(&image);
-			p.fillRect(0, 0, image.width(), image.height(), b);
-			p.end();
+			color.setHsv(360 * x / xSize, 256 * (ySize - 1 - y) / ySize, val);
+			*p = color.rgb();
+			++p;
 		}
-		QColor color;
-		double L = val /  2.55;
-		for (int y = 0; y < 128; y++)
+	}
+	m_pixmap = QPixmap::fromImage(ProofImage(&image, m_doc));
+}
+
+void ColorChart::drawLabColorMap(int val)
+{
+	int xSize = width();
+	int ySize = height();
+
+	QImage image(128, 128, QImage::Format_ARGB32);
+	bool doSoftProofing = m_doc ? m_doc->SoftProofing : false;
+	bool doGamutCheck   = m_doc ? m_doc->Gamut : false;
+	if (doSoftProofing && doGamutCheck)
+	{
+		QPainter p;
+		QBrush b(QColor(205, 205, 205), IconManager::instance().loadPixmap("testfill.png"));
+		p.begin(&image);
+		p.fillRect(0, 0, image.width(), image.height(), b);
+		p.end();
+	}
+	QColor color;
+	double L = val /  2.55;
+	for (int y = 0; y < 128; y++)
+	{
+		unsigned int* p = reinterpret_cast<unsigned int*>(image.scanLine(y));
+		for (int x = 0; x < 128; x++)
 		{
-			unsigned int* p = reinterpret_cast<unsigned int*>(image.scanLine(y));
-			for (int x = 0; x < 128; x++)
+			double yy = 256 - (y * 2.0);
+			if (doSoftProofing && doGamutCheck)
 			{
-				double yy = 256 - (y * 2.0);
-				if (doSoftProofing && doGamutCheck)
-				{
-					bool outOfG = false;
-					color = ScColorEngine::getDisplayColorGC(ScColor(L, (x * 2.0) - 128.0, yy - 128.0), m_doc, &outOfG);
-					if (!outOfG)
-						*p = color.rgb();
-				}
-				else
-				{
-					color = ScColorEngine::getDisplayColor(ScColor(L, (x * 2.0) - 128.0, yy - 128.0), m_doc);
+				bool outOfG = false;
+				color = ScColorEngine::getDisplayColorGC(ScColor(L, (x * 2.0) - 128.0, yy - 128.0), m_doc, &outOfG);
+				if (!outOfG)
 					*p = color.rgb();
-				}
-				++p;
 			}
+			else
+			{
+				color = ScColorEngine::getDisplayColor(ScColor(L, (x * 2.0) - 128.0, yy - 128.0), m_doc);
+				*p = color.rgb();
+			}
+			++p;
 		}
-		m_pixmap = QPixmap::fromImage(image.scaled(xSize, ySize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
-	}
-	else
-	{
-		QImage image(xSize, ySize, QImage::Format_ARGB32_Premultiplied);
-		QColor color;
-		for (int y = 0; y < ySize; ++y)
-		{
-			unsigned int* p = reinterpret_cast<unsigned int*>(image.scanLine(y));
-			for(int x = 0; x < xSize; ++x)
-			{
-				color.setHsv(360 * x / xSize, 256 * (ySize - 1 - y) / ySize, val);
-				*p = color.rgb();
-				++p;
-			}
-		}
-		m_pixmap = QPixmap::fromImage(ProofImage(&image, m_doc));
-	}
-	update();
-}
+	}
+	m_pixmap = QPixmap::fromImage(image.scaled(xSize, ySize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
+}

Modified: trunk/Scribus/scribus/ui/colorchart.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=24407&path=/trunk/Scribus/scribus/ui/colorchart.h
==============================================================================
--- trunk/Scribus/scribus/ui/colorchart.h	(original)
+++ trunk/Scribus/scribus/ui/colorchart.h	Sat Jan 16 21:42:34 2021
@@ -24,12 +24,14 @@
 #ifndef COLORCHART_H
 #define COLORCHART_H
 
+#include <QPixmap>
 #include <QWidget>
-#include <QPixmap>
-#include <QPaintEvent>
-#include <QMouseEvent>
+
 #include "scribusapi.h"
 
+class QMouseEvent;
+class QPaintEvent;
+class QResizeEvent;
 class ScribusDoc;
 
 /**
@@ -38,18 +40,12 @@
 
 class SCRIBUS_API ColorChart : public QWidget
 {
-
-Q_OBJECT
+	Q_OBJECT
 
 public:
 	ColorChart(QWidget *parent);
 	ColorChart(QWidget *parent, ScribusDoc* doc);
 	~ColorChart() {};
-
-	void mouseMoveEvent(QMouseEvent *m);
-	void mousePressEvent(QMouseEvent *m);
-	void mouseReleaseEvent(QMouseEvent *m);
-	void paintEvent(QPaintEvent *e);
 
 	void setDoc(ScribusDoc* doc) { m_doc = doc; }
 	void drawMark(int x, int y);
@@ -61,12 +57,22 @@
 protected:
 	ScribusDoc* m_doc;
 
+	int m_currentValue { 255 };
 	int m_markX { 0 };
 	int m_markY { 0 };
 	bool m_drawMark {false};
 	
 	QPixmap m_pixmap;
 
+	void drawHsvColorMap(int val);
+	void drawLabColorMap(int val);
+
+	void mouseMoveEvent(QMouseEvent *m);
+	void mousePressEvent(QMouseEvent *m);
+	void mouseReleaseEvent(QMouseEvent *m);
+	void paintEvent(QPaintEvent *e);
+	void resizeEvent(QResizeEvent *e);
+
 signals:
 	void ColorVal(int h, int s, bool ende);
 };




More information about the scribus-commit mailing list