17 #include <QVBoxLayout>
20 #include <QResizeEvent>
21 #include <QStyleOption>
24 #include "pencildef.h"
26 #include "colorwheel.h"
31 mCurrentColor = mCurrentColor.toHsv();
40 void ColorWheel::setColor(
QColor color)
45 color = color.
toHsv();
47 if (color == mCurrentColor)
52 if (color.
hue() == -1)
57 mCurrentColor = color;
59 drawSquareImage(color.
hue());
75 hue = qAtan2(-diff.
y(), diff.
x()) / M_PI * 180;
76 hue = fmod((hue + 360), 360);
84 hue = (hue > 359) ? 359 : hue;
85 hue = (hue < 0) ? 0 : hue;
89 mCurrentColor.
value());
98 p.
x() / (mSquareRect.
width() - 1),
99 1.0 - (p.
y() / (mSquareRect.
height()-1)));
104 void ColorWheel::mousePressEvent(
QMouseEvent *event)
106 QPoint lastPos =
event->pos();
111 QColor color = pickColor(lastPos);
113 valueChanged(color.
value());
116 else if (mWheelRect.
contains(lastPos))
120 QColor color = pickColor(lastPos);
121 hueChanged(color.
hue());
125 void ColorWheel::mouseMoveEvent(
QMouseEvent* event)
127 QPoint lastPos =
event->pos();
134 if (lastPos.
x() < mSquareRect.
topLeft().
x())
143 if (lastPos.
y() < mSquareRect.
topLeft().
y())
152 QColor color = pickColor(lastPos);
154 valueChanged(color.
value());
156 else if (mWheelRect.
contains(lastPos) && mIsInWheel)
158 QColor color = pickColor(lastPos);
159 hueChanged(color.
hue());
167 emit colorSelected(mCurrentColor);
174 drawWheelImage(event->
size());
175 drawSquareImage(mCurrentColor.
hue());
188 composeWheel(mWheelPixmap);
196 void ColorWheel::drawWheelImage(
const QSize &newSize)
203 QBrush backgroundBrush = option.palette.window();
214 for (
int hue = 0; hue < 360; hue += 1)
216 conicalGradient.setColorAt(hue / 360.0,
QColor::fromHsv(hue, 255, 255));
219 qreal ir = r - mWheelThickness;
224 QBrush brush(conicalGradient);
231 painter.
drawEllipse(
QPoint(0, 0), r / 2 - mWheelThickness, r / 2 - mWheelThickness);
234 qreal m1 = (mWheelPixmap.
width() / 2) - (ir / qSqrt(2));
235 qreal m2 = (mWheelPixmap.
height() / 2) - (ir / qSqrt(2));
238 qreal wheelWidth = 2 * ir / qSqrt(2);
244 void ColorWheel::drawSquareImage(
const int &hue)
251 qreal ir = r - mWheelThickness;
254 qreal m1 = (
width() / 2) - (ir / qSqrt(2.1));
255 qreal m2 = (
height() / 2) - (ir / qSqrt(2.1));
274 painter.
fillRect(square.rect(), colorGradiantBrush);
275 painter.
fillRect(square.rect(), blackGradiantBrush);
277 qreal SquareWidth = 2 * ir / qSqrt(2.1);
279 mSquareImage = square.
scaled(mSquareRect.
size());
283 void ColorWheel::drawHueIndicator(
const int &hue)
287 if (hue > 20 && hue < 200)
304 r = r / 2.0 - mWheelThickness / 2;
308 void ColorWheel::drawPicker(
const QColor& color)
329 transform.
translate(-ellipseSize/2,-ellipseSize/2);
330 transform.
translate(squareTopLeft.
x(),squareTopLeft.
y()-1);
332 painter.
drawEllipse(static_cast<int>(S), static_cast<int>(V), ellipseSize, ellipseSize);
335 void ColorWheel::composeWheel(
QPixmap& pixmap)
338 composePainter.drawImage(0, 0, mWheelImage);
339 composePainter.drawImage(mSquareRect, mSquareImage);
340 composePainter.end();
341 drawHueIndicator(mCurrentColor.
hsvHue());
342 drawPicker(mCurrentColor);
345 void ColorWheel::hueChanged(
const int &hue)
347 if (hue < 0 || hue > 359)
352 int v = mCurrentColor.
value();
353 int a = mCurrentColor.
alpha();
355 mCurrentColor.
setHsv(hue, s, v, a);
362 drawSquareImage(hue);
365 emit colorChanged(mCurrentColor);
368 void ColorWheel::saturationChanged(
const int &sat)
370 int hue = mCurrentColor.
hsvHue();
371 int value = mCurrentColor.
value();
372 int alpha = mCurrentColor.
alpha();
374 mCurrentColor.
setHsv(hue, sat, value, alpha);
377 emit colorChanged(mCurrentColor);
380 void ColorWheel::valueChanged(
const int &value)
382 int hue = mCurrentColor.
hsvHue();
384 int alpha = mCurrentColor.
alpha();
385 mCurrentColor.
setHsv(hue, sat, value, alpha);
388 emit colorChanged(mCurrentColor);
void fillRect(const QRectF &rectangle, const QBrush &brush)
void setRenderHint(QPainter::RenderHint hint, bool on)
void fill(const QColor &color)
void setColorAt(qreal position, const QColor &color)
QPoint bottomRight() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QColor toHsv() const const
QColor fromHsv(int h, int s, int v, int a)
void initFrom(const QWidget *widget)
void setPen(const QColor &color)
void drawEllipse(const QRectF &rectangle)
qreal valueF() const const
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QTextStream & center(QTextStream &stream)
void setBrush(const QBrush &brush)
void setColor(const QColor &color)
const QSize & size() const const
bool contains(const QRect &rectangle, bool proper) const const
qreal hsvSaturationF() const const
QPoint topLeft() const const
QRect toAlignedRect() const const
void translate(const QPointF &offset)
virtual void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const const =0
void setHsv(int h, int s, int v, int a)
int saturation() const const
bool begin(QPaintDevice *device)
const QPen & pen() const const
QImage scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
QColor fromHsvF(qreal h, qreal s, qreal v, qreal a)
int hsvSaturation() const const