Commit a34c47bd authored by Dmitry Kazakov's avatar Dmitry Kazakov

Ported the rest of the color selectors to use KisDisplayColorConverter

parent cf3c9a4f
......@@ -30,6 +30,10 @@ namespace Acs {
return button == Qt::LeftButton ? Acs::Foreground : Acs::Background;
}
inline ColorRole buttonsToRole(Qt::MouseButton button, Qt::MouseButtons buttons) {
return button == Qt::LeftButton || buttons & Qt::LeftButton ? Acs::Foreground : Acs::Background;
}
template <class ResourceProvider>
void setCurrentColor(ResourceProvider *provider, ColorRole role, const KoColor &color) {
if (role == Acs::Foreground) {
......@@ -53,7 +57,7 @@ namespace Acs {
template <class PaintDeviceSP>
void setColor(PaintDeviceSP device, const QPoint &pt, const KoColor &color) {
(void) device->setPixel(pt.x(), pt.y(), &color);
(void) device->setPixel(pt.x(), pt.y(), color);
}
}
......
......@@ -49,7 +49,7 @@ void KisColorHistory::setCanvas(KisCanvas2 *canvas)
m_resourceProvider = canvas->view()->resourceProvider();
connect(canvas->view()->resourceProvider(), SIGNAL(sigFGColorUsed(KoColor)),
this, SLOT(commitColor(KoColor)), Qt::UniqueConnection);
this, SLOT(addColorToHistory(KoColor)), Qt::UniqueConnection);
}
KisColorSelectorBase* KisColorHistory::createPopup() const
......@@ -61,7 +61,7 @@ KisColorSelectorBase* KisColorHistory::createPopup() const
return ret;
}
void KisColorHistory::commitColor(const KoColor& color)
void KisColorHistory::addColorToHistory(const KoColor& color)
{
// don't add color in erase mode. See https://bugs.kde.org/show_bug.cgi?id=298940
if (m_resourceProvider && m_resourceProvider->currentCompositeOp() == COMPOSITE_ERASE) return;
......
......@@ -35,7 +35,7 @@ protected:
KisColorSelectorBase* createPopup() const;
public slots:
void commitColor(const KoColor& color);
void addColorToHistory(const KoColor& color);
private:
QList<KoColor> m_colorHistory;
......
......@@ -55,6 +55,7 @@ KisColorSelector::KisColorSelector(Configuration conf, QWidget* parent)
m_blipDisplay(true)
{
init();
updateSettings();
setConfiguration(conf);
}
......@@ -78,7 +79,7 @@ KisColorSelector::KisColorSelector(QWidget* parent)
KisColorSelectorBase* KisColorSelector::createPopup() const
{
KisColorSelectorBase* popup = new KisColorSelector(0);
popup->setColor(m_lastColor);
popup->setKoColor(m_lastRealColor);
return popup;
}
......@@ -223,7 +224,7 @@ void KisColorSelector::resizeEvent(QResizeEvent* e) {
}
// reset the currect color after resizing the widget
setColor(m_lastColor);
setKoColor(m_lastRealColor);
KisColorSelectorBase::resizeEvent(e);
}
......@@ -258,13 +259,12 @@ void KisColorSelector::mouseReleaseEvent(QMouseEvent* e)
KisColorSelectorBase::mousePressEvent(e);
if(!e->isAccepted() &&
m_lastColor != m_currentColor &&
m_currentColor.isValid()) {
!(m_lastRealColor == m_currentRealColor)) {
m_lastColor=m_currentColor;
m_lastRealColor = m_currentRealColor;
m_lastColorRole = Acs::buttonToRole(e->button());
commitColor(KoColor(m_currentColor, colorSpace()), m_lastColorRole);
updateColor(m_lastRealColor, m_lastColorRole, false);
e->accept();
}
......@@ -273,17 +273,14 @@ void KisColorSelector::mouseReleaseEvent(QMouseEvent* e)
bool KisColorSelector::displaySettingsButton()
{
if(dynamic_cast<KisColorSelectorContainer*>(parent())!=0)
return true;
else
return false;
return dynamic_cast<KisColorSelectorContainer*>(parent());
}
void KisColorSelector::setColor(const QColor &color)
void KisColorSelector::setKoColor(const KoColor &color)
{
m_mainComponent->setColor(color);
m_subComponent->setColor(color);
m_lastColor=color;
m_mainComponent->setKoColor(color);
m_subComponent->setKoColor(color);
m_lastRealColor = color;
update();
}
......@@ -292,13 +289,13 @@ void KisColorSelector::mouseEvent(QMouseEvent *e)
if (m_grabbingComponent && (e->buttons() & Qt::LeftButton || e->buttons() & Qt::RightButton)) {
m_grabbingComponent->mouseEvent(e->x(), e->y());
m_currentColor=m_mainComponent->currentColor();
KoColor kocolor(m_currentColor, colorSpace());
updateColorPreview(kocolor);
Acs::ColorRole role = Acs::buttonToRole(e->button());
commitColor(kocolor, role);
KoColor color = m_mainComponent->currentColor();
m_currentRealColor = color;
updateColorPreview(color);
Acs::ColorRole role = Acs::buttonsToRole(e->button(), e->buttons());
updateColor(color, role, false);
}
}
......
......@@ -20,7 +20,6 @@
#include "kis_color_selector_base.h"
#include <QColor>
class KisColorSelectorTriangle;
class KisColorSelectorRing;
class KisColorSelectorComponent;
......@@ -94,7 +93,7 @@ public:
void setConfiguration(Configuration conf);
Configuration configuration() const;
void setColor(const QColor& color);
void setKoColor(const KoColor &color);
public slots:
void updateSettings();
......@@ -129,8 +128,8 @@ private:
Configuration m_configuration;
QColor m_lastColor;
QColor m_currentColor;
KoColor m_lastRealColor;
KoColor m_currentRealColor;
bool m_blipDisplay;
Acs::ColorRole m_lastColorRole;
......
......@@ -286,17 +286,6 @@ void KisColorSelectorBase::keyPressEvent(QKeyEvent *)
}
}
QColor KisColorSelectorBase::findGeneratingColor(const KoColor& ref) const
{
KoColor color(ref);
QColor result;
color.convertTo(colorSpace());
color.toQColor(&result);
return result;
}
void KisColorSelectorBase::dragEnterEvent(QDragEnterEvent *e)
{
if(e->mimeData()->hasColor())
......@@ -330,14 +319,9 @@ void KisColorSelectorBase::updateColor(const KoColor &color, Acs::ColorRole role
}
}
void KisColorSelectorBase::setColor(const QColor& color)
{
Q_UNUSED(color);
}
void KisColorSelectorBase::setKoColor(const KoColor& color)
{
setColor(color.toQColor());
Q_UNUSED(color);
}
void KisColorSelectorBase::setHidingTime(int time)
......@@ -347,7 +331,6 @@ void KisColorSelectorBase::setHidingTime(int time)
m_hideTimer->setInterval(time);
}
void KisColorSelectorBase::lazyCreatePopup()
{
if (!m_popup) {
......
......@@ -55,21 +55,12 @@ public:
void updateColorPreview(const KoColor &color);
void showColorPreview();
// FIXME: make private and use updateColor instead
virtual void setColor(const QColor& color);
virtual void setKoColor(const KoColor& color);
// FIXME: deprecate
QColor findGeneratingColor(const KoColor& ref) const;
public slots:
virtual void updateSettings();
virtual void showPopup(Move move=MoveToMousePosition);
//FIXME: make private and use updateColor instead
/// commits a color to the resource manager
void commitColor(const KoColor& koColor, Acs::ColorRole role);
public:
void enterEvent(QEvent *e);
void leaveEvent(QEvent *e);
......@@ -85,6 +76,10 @@ protected:
void setHidingTime(int time);
bool isPopup() const { return m_isPopup; }
private:
void commitColor(const KoColor& koColor, Acs::ColorRole role);
protected slots:
void hidePopup();
......
......@@ -67,7 +67,7 @@ public:
item->setMinimumSize(selectorSize, selectorSize);
item->setMouseTracking(true);
item->setEnabled(false);
item->setColor(QColor(255,0,0));
item->setKoColor(KoColor(QColor(255,0,0), item->colorSpace()));
item->setDisplayBlip(false);
}
}
......@@ -124,7 +124,7 @@ KisColorSelectorComboBox::KisColorSelectorComboBox(QWidget* parent) :
layout->addWidget(&m_currentSelector);
m_currentSelector.setEnabled(false);
m_currentSelector.setDisplayBlip(false);
m_currentSelector.setColor(QColor(255,0,0));
m_currentSelector.setKoColor(KoColor(QColor(255,0,0), m_currentSelector.colorSpace()));
// 30 pixels for the arrow of the combobox
setMinimumSize(m_private->selectorSize+m_private->spacing+30,m_private->selectorSize+m_private->spacing);
......@@ -177,6 +177,6 @@ void KisColorSelectorComboBox::setConfiguration(KisColorSelector::Configuration
{
m_configuration=conf;
m_currentSelector.setConfiguration(conf);
m_currentSelector.setColor(QColor(255,0,0));
m_currentSelector.setKoColor(KoColor(QColor(255,0,0), m_currentSelector.colorSpace()));
update();
}
......@@ -69,7 +69,7 @@ void KisColorSelectorComponent::mouseEvent(int x, int y)
int newX=qBound(0, (x-m_x), width());
int newY=qBound(0, (y-m_y), height());
selectColor(newX, newY);
selectKoColor(newX, newY);
m_lastX=newX;
m_lastY=newY;
}
......@@ -94,10 +94,9 @@ bool KisColorSelectorComponent::containsPointInComponentCoords(int x, int y) con
return false;
}
QColor KisColorSelectorComponent::currentColor()
KoColor KisColorSelectorComponent::currentColor()
{
return selectColor(m_lastX, m_lastY);
return selectKoColor(m_lastX, m_lastY);
}
void KisColorSelectorComponent::setParam(qreal hue, qreal hsvSaturation, qreal value, qreal hslSaturation, qreal lightness)
......
......@@ -44,8 +44,8 @@ public:
void mouseEvent(int x, int y);
/// return the color, that was selected by calling mouseEvent
/// the color must not have a color space conversion
virtual QColor currentColor();
KoColor currentColor();
int width() const;
int height() const;
......@@ -53,14 +53,10 @@ public:
void setConfiguration(Parameter param, Type type);
/// set the color, blibs etc
virtual void setColor(const QColor& color) = 0;
virtual void setKoColor(const KoColor& color) = 0;
/// returns true, if this component wants to grab the mouse (normaly true, if containsPoint returns true)
virtual bool wantsGrab(int x, int y) {return containsPointInComponentCoords(x-m_x, y-m_y);}
/// returns true, if the component contains the given point
bool containsPoint(int x, int y) const {return containsPointInComponentCoords(x-m_x, y-m_y);}
bool containsPoint(const QPoint &point) const {return containsPointInComponentCoords(point.x()-m_x, point.y()-m_y);}
bool wantsGrab(int x, int y) {return containsPointInComponentCoords(x-m_x, y-m_y);}
public slots:
/// set hue, saturation, value or/and lightness
......@@ -77,7 +73,7 @@ protected:
bool isDirty() const;
/// this method must be overloaded to return the colour at position x/y and draw a marker on that position
virtual QColor selectColor(int x, int y) = 0;
virtual KoColor selectKoColor(int x, int y) = 0;
/// paint component using given painter
/// the component should respect width() and height() (eg. scale to width and height), but doesn't
......
......@@ -25,6 +25,8 @@ USING_PART_OF_NAMESPACE_EIGEN
#include <cmath>
#include "KoColor.h"
#include "kis_display_color_converter.h"
KisColorSelectorRing::KisColorSelectorRing(KisColorSelector *parent) :
KisColorSelectorComponent(parent),
......@@ -100,7 +102,8 @@ void KisColorSelectorRing::paint(QPainter* painter)
}
}
QColor KisColorSelectorRing::selectColor(int x, int y) {
KoColor KisColorSelectorRing::selectKoColor(int x, int y)
{
QPoint ringMiddle(width()/2, height()/2);
QPoint ringCoord = QPoint(x, y)-ringMiddle;
qreal hue = std::atan2(qreal(ringCoord.y()), qreal(ringCoord.x()))+(M_PI);
......@@ -109,18 +112,21 @@ QColor KisColorSelectorRing::selectColor(int x, int y) {
m_lastHue=hue;
emit update();
return QColor();
return KoColor();
}
void KisColorSelectorRing::setColor(const QColor &color)
void KisColorSelectorRing::setKoColor(const KoColor &color)
{
qreal h, s, v;
m_parent->converter()->getHsvF(color, &h, &s, &v);
emit paramChanged(h, -1, -1, -1, -1);
// selector keeps the position on the ring if hue is undefined (when saturation is 0)
if (!qFuzzyCompare(color.saturationF(), 0.0)) {
emit paramChanged(color.hueF(), -1, -1, -1, -1);
m_lastHue=color.hueF();
} else {
emit paramChanged(m_lastHue, -1, -1, -1, -1);
if (!qFuzzyCompare(s, 0.0)) {
m_lastHue=h;
}
emit update();
}
......
......@@ -28,16 +28,18 @@ class KisColorSelectorRing : public KisColorSelectorComponent
public:
explicit KisColorSelectorRing(KisColorSelector *parent);
int innerRadius() const;
QColor selectColor(int x, int y);
void setColor(const QColor &color);
void setKoColor(const KoColor &color);
protected:
void paint(QPainter*);
KoColor selectKoColor(int x, int y);
bool containsPointInComponentCoords(int x, int y) const;
private:
void paintCache();
void colorCache();
int outerRadius() const;
QImage m_pixelCache;
const KoColorSpace* m_cachedColorSpace;
int m_cachedSize;
......
......@@ -20,6 +20,8 @@
#include <QPainter>
#include <QColor>
#include <cmath>
#include "kis_display_color_converter.h"
KisColorSelectorSimple::KisColorSelectorSimple(KisColorSelector *parent) :
KisColorSelectorComponent(parent),
......@@ -27,87 +29,8 @@ KisColorSelectorSimple::KisColorSelectorSimple(KisColorSelector *parent) :
{
}
void KisColorSelectorSimple::setColor(const QColor &c)
{
switch (m_parameter) {
case KisColorSelector::SL:
m_lastClickPos.setX(c.hslSaturationF());
m_lastClickPos.setY(1 - c.lightnessF());
emit paramChanged(-1, -1, -1, c.hslSaturationF(), c.lightnessF());
break;
case KisColorSelector::LH:
m_lastClickPos.setX(qBound<qreal>(0., c.hueF(), 1.));
m_lastClickPos.setY(1 - c.lightnessF());
emit paramChanged(c.hueF(), -1, -1, -1, c.lightnessF());
break;
case KisColorSelector::SV:
m_lastClickPos.setX(c.saturationF());
m_lastClickPos.setY(1 - c.valueF());
emit paramChanged(-1, c.saturationF(), c.valueF(), -1, -1);
break;
case KisColorSelector::SV2: {
qreal xRel = c.hsvSaturationF();
qreal yRel = 0.5;
if(xRel != 1.0)
yRel = 1.0 - qBound<qreal>(0.0, (c.valueF() - xRel) / (1.0 - xRel), 1.0);
m_lastClickPos.setX(xRel);
m_lastClickPos.setY(yRel);
emit paramChanged(-1, -1, -1, xRel, yRel);
break;
}
case KisColorSelector::VH:
m_lastClickPos.setX(qBound<qreal>(0., c.hueF(), 1.));
m_lastClickPos.setY(1 - c.valueF());
emit paramChanged(c.hueF(), -1, c.valueF(), -1, -1);
break;
case KisColorSelector::hsvSH:
m_lastClickPos.setX(qBound<qreal>(0., c.hueF(), 1.));
m_lastClickPos.setY(1 - c.saturationF());
emit paramChanged(c.hueF(), c.saturationF(), -1, -1, -1);
break;
case KisColorSelector::hslSH:
m_lastClickPos.setX(qBound<qreal>(0., c.hslHueF(), 1.));
m_lastClickPos.setY(1 - c.hslSaturationF());
emit paramChanged(c.hueF(), -1, -1, c.hslSaturationF(), -1);
break;
case KisColorSelector::L:
m_lastClickPos.setX(qBound<qreal>(0., c.lightnessF(), 1.));
emit paramChanged(-1, -1, -1, -1, c.lightnessF());
break;
case KisColorSelector::V:
m_lastClickPos.setX(c.valueF());
emit paramChanged(-1, -1, c.valueF(), -1, -1);
break;
case KisColorSelector::hsvS:
m_lastClickPos.setX( c.saturationF() );
emit paramChanged(-1, c.saturationF(), -1, -1, -1);
break;
case KisColorSelector::hslS:
m_lastClickPos.setX( c.hslSaturationF() );
emit paramChanged(-1, -1, -1, c.hslSaturationF(), -1);
break;
case KisColorSelector::H:
m_lastClickPos.setX(qBound<qreal>(0., c.hueF(), 1.));
emit paramChanged(c.hueF(), -1, -1, -1, -1);
break;
default:
Q_ASSERT(false);
break;
}
emit update();
//Workaround for bug 317648
setLastMousePosition((m_lastClickPos.x()*width()), (m_lastClickPos.y()*height()));
}
QColor KisColorSelectorSimple::selectColor(int x, int y)
KoColor KisColorSelectorSimple::selectKoColor(int x, int y)
{
m_kocolor.convertTo(colorSpace());
m_lastClickPos.setX(x/qreal(width()));
m_lastClickPos.setY(y/qreal(height()));
......@@ -157,22 +80,100 @@ QColor KisColorSelectorSimple::selectColor(int x, int y)
}
emit update();
return colorAt(x, y);
}
void KisColorSelectorSimple::setKoColor(const KoColor &color)
{
qreal hsvH, hsvS, hsvV;
qreal hslH, hslS, hslL;
m_parent->converter()->getHsvF(color, &hsvH, &hsvS, &hsvV);
m_parent->converter()->getHslF(color, &hslH, &hslS, &hslL);
switch (m_parameter) {
case KisColorSelector::SL:
m_lastClickPos.setX(hslS);
m_lastClickPos.setY(1 - hslL);
emit paramChanged(-1, -1, -1, hslS, hslL);
break;
case KisColorSelector::LH:
m_lastClickPos.setX(qBound<qreal>(0., hslH, 1.));
m_lastClickPos.setY(1 - hslL);
emit paramChanged(hslH, -1, -1, -1, hslL);
break;
case KisColorSelector::SV:
m_lastClickPos.setX(hsvS);
m_lastClickPos.setY(1 - hsvV);
emit paramChanged(-1, hsvS, hsvV, -1, -1);
break;
case KisColorSelector::SV2: {
qreal xRel = hsvS;
qreal yRel = 0.5;
if(xRel != 1.0)
yRel = 1.0 - qBound<qreal>(0.0, (hsvV - xRel) / (1.0 - xRel), 1.0);
m_lastClickPos.setX(xRel);
m_lastClickPos.setY(yRel);
emit paramChanged(-1, -1, -1, xRel, yRel);
break;
}
case KisColorSelector::VH:
m_lastClickPos.setX(qBound<qreal>(0., hsvH, 1.));
m_lastClickPos.setY(1 - hsvV);
emit paramChanged(hsvH, -1, hsvV, -1, -1);
break;
case KisColorSelector::hsvSH:
m_lastClickPos.setX(qBound<qreal>(0., hsvH, 1.));
m_lastClickPos.setY(1 - hsvS);
emit paramChanged(hsvH, hsvS, -1, -1, -1);
break;
case KisColorSelector::hslSH:
m_lastClickPos.setX(qBound<qreal>(0., hslH, 1.));
m_lastClickPos.setY(1 - hslS);
emit paramChanged(hslH, -1, -1, hslS, -1);
break;
case KisColorSelector::L:
m_lastClickPos.setX(qBound<qreal>(0., hslL, 1.));
emit paramChanged(-1, -1, -1, -1, hslL);
break;
case KisColorSelector::V:
m_lastClickPos.setX(hsvV);
emit paramChanged(-1, -1, hsvV, -1, -1);
break;
case KisColorSelector::hsvS:
m_lastClickPos.setX( hsvS );
emit paramChanged(-1, hsvS, -1, -1, -1);
break;
case KisColorSelector::hslS:
m_lastClickPos.setX( hslS );
emit paramChanged(-1, -1, -1, hslS, -1);
break;
case KisColorSelector::H:
m_lastClickPos.setX(qBound<qreal>(0., hsvH, 1.));
emit paramChanged(hsvH, -1, -1, -1, -1);
break;
default:
Q_ASSERT(false);
break;
}
emit update();
//Workaround for bug 317648
setLastMousePosition((m_lastClickPos.x()*width()), (m_lastClickPos.y()*height()));
}
void KisColorSelectorSimple::paint(QPainter* painter)
{
if(isDirty()) {
m_kocolor.convertTo(colorSpace());
KoColor color;
m_pixelCache=QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
for(int x=0; x<width(); x++) {
for(int y=0; y<height(); y++) {
m_kocolor.fromQColor(colorAt(x, y));
m_kocolor.toQColor(&m_qcolor);
m_pixelCache.setPixel(x, y, m_qcolor.rgb());
color = colorAt(x, y);
QColor c = m_parent->converter()->toQColor(color);
m_pixelCache.setPixel(x, y, c.rgba());
}
}
}
......@@ -217,7 +218,7 @@ void KisColorSelectorSimple::paint(QPainter* painter)
}
}
const QColor& KisColorSelectorSimple::colorAt(int x, int y)
KoColor KisColorSelectorSimple::colorAt(int x, int y)
{
qreal xRel = x/qreal(width());
qreal yRel = 1.-y/qreal(height());
......@@ -227,51 +228,50 @@ const QColor& KisColorSelectorSimple::colorAt(int x, int y)
else
relPos = x/qreal(width());
QColor color;
color.setHsvF(m_hue, 1.0, 1.0);
KoColor color(Qt::transparent, m_parent->colorSpace());
switch(m_parameter) {
case KisColorSelector::SL:
m_qcolor.setHslF(m_hue, xRel, yRel);
color = m_parent->converter()->fromHslF(m_hue, xRel, yRel);
break;
case KisColorSelector::SV:
m_qcolor.setHsvF(m_hue, xRel, yRel);
color = m_parent->converter()->fromHsvF(m_hue, xRel, yRel);