20#include <QResizeEvent>
21#include <QStyleOption>
26#include "colorwheel.h"
30 setWindowTitle(tr(
"Color Wheel",
"Color Wheel's window title"));
31 mCurrentColor = mCurrentColor.toHsv();
32 setMinimumHeight(100);
40void 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)));
104void 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());
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);
196void 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);
244void 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());
283void ColorWheel::drawHueIndicator(
const int &hue)
287 if (hue > 20 && hue < 200)
304 r = r / 2.0 - mWheelThickness / 2;
308void 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);
335void 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);
345void 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);
368void 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);
380void 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);
QColor fromHsv(int h, int s, int v, int a)
QColor fromHsvF(qreal h, qreal s, qreal v, qreal a)
int hsvSaturation() const const
qreal hsvSaturationF() const const
int saturation() const const
void setHsv(int h, int s, int v, int a)
QColor toHsv() const const
qreal valueF() const const
void setColorAt(qreal position, const QColor &color)
QImage scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
bool begin(QPaintDevice *device)
void drawEllipse(const QRectF &rectangle)
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
void fillRect(const QRectF &rectangle, const QBrush &brush)
const QPen & pen() const const
void setBrush(const QBrush &brush)
void setPen(const QColor &color)
void setRenderHint(QPainter::RenderHint hint, bool on)
void translate(const QPointF &offset)
void setColor(const QColor &color)
void fill(const QColor &color)
QPoint bottomRight() const const
bool contains(const QRect &rectangle, bool proper) const const
QPoint topLeft() const const
QRect toAlignedRect() const const
virtual void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const const=0
void initFrom(const QWidget *widget)
QTextStream & center(QTextStream &stream)