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,
event->rect());
196void ColorWheel::drawWheelImage(
const QSize &newSize)
203 QBrush backgroundBrush = option.palette.window();
213 for (
int hue = 0; hue < 360; hue += 1)
215 conicalGradient.setColorAt(hue / 360.0,
QColor::fromHsv(hue, 255, 255));
218 qreal ir = r - mWheelThickness;
223 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,
QRect blitRect)
339 composePainter.setClipRect(blitRect);
342 composePainter.drawImage(0, 0, mWheelImage);
343 composePainter.drawImage(mSquareRect, mSquareImage);
344 composePainter.end();
345 drawHueIndicator(mCurrentColor.
hsvHue());
346 drawPicker(mCurrentColor);
349void ColorWheel::hueChanged(
const int &hue)
351 if (hue < 0 || hue > 359)
356 int v = mCurrentColor.
value();
357 int a = mCurrentColor.
alpha();
359 mCurrentColor.
setHsv(hue, s, v, a);
366 drawSquareImage(hue);
369 emit colorChanged(mCurrentColor);
372void ColorWheel::saturationChanged(
const int &sat)
374 int hue = mCurrentColor.
hsvHue();
375 int value = mCurrentColor.
value();
376 int alpha = mCurrentColor.
alpha();
378 mCurrentColor.
setHsv(hue, sat, value, alpha);
381 emit colorChanged(mCurrentColor);
384void ColorWheel::valueChanged(
const int &value)
386 int hue = mCurrentColor.
hsvHue();
388 int alpha = mCurrentColor.
alpha();
389 mCurrentColor.
setHsv(hue, sat, value, alpha);
392 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)
void fill(uint pixelValue)
QImage scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
CompositionMode_SourceOver
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 setCompositionMode(QPainter::CompositionMode mode)
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)