25 #include <QMouseEvent> 27 #include <QPainterPath> 29 #include <QDragEnterEvent> 63 : w(widget), hue(0), sat(0), val(0),
64 wheel_width(20), mouse_status(
Nothing),
72 return qMin(w->geometry().width(), w->geometry().height())/2;
102 return QLineF (w->geometry().width()/2, w->geometry().height()/2, p.x(), p.y());
108 QSize size(width, width);
109 inner_selector = QImage(size, QImage::Format_RGB32);
111 for (
int y = 0; y < width; ++y )
113 for (
int x = 0; x < width; ++x )
115 inner_selector.setPixel( x, y,
116 color_from(hue,
double(x)/width,
double(y)/width,1).rgb());
129 size *= max_size / size.height();
131 qreal ycenter = size.height()/2;
132 inner_selector = QImage(size.toSize(), QImage::Format_RGB32);
134 for (
int x = 0; x < inner_selector.width(); x++ )
136 qreal pval = x / size.height();
137 qreal slice_h = size.height() * pval;
138 for (
int y = 0; y < inner_selector.height(); y++ )
140 qreal ymin = ycenter-slice_h/2;
141 qreal psat = qBound(0.0,(y-ymin)/slice_h,1.0);
143 inner_selector.setPixel(x,y,
color_from(hue,psat,pval,1).rgb());
198 hue_ring.fill(Qt::transparent);
199 QPainter painter(&hue_ring);
200 painter.setRenderHint(QPainter::Antialiasing);
201 painter.setCompositionMode(QPainter::CompositionMode_Source);
204 const int hue_stops = 24;
205 QConicalGradient gradient_hue(0, 0, 0);
206 if ( gradient_hue.stops().size() < hue_stops )
208 for (
double a = 0; a < 1.0; a+=1.0/(hue_stops-1) )
217 painter.setPen(Qt::NoPen);
218 painter.setBrush(QBrush(gradient_hue));
221 painter.setBrush(Qt::transparent);
229 hue = qMax(0.0, c.hsvHueF());
230 sat = c.hsvSaturationF();
235 hue = qMax(0.0, c.hueF());
241 hue = qMax(0.0, c.hsvHueF());
249 QWidget(parent),
p(new
Private(this))
252 setAcceptDrops(
true);
273 return color().hueF();
279 return color().hsvSaturationF();
284 return color().valueF();
301 QPainter painter(
this);
302 painter.setRenderHint(QPainter::Antialiasing);
303 painter.translate(geometry().width()/2,geometry().height()/2);
312 painter.setPen(QPen(Qt::black,3));
313 painter.setBrush(Qt::NoBrush);
315 ray.setAngle(
p->
hue*360);
316 QPointF h1 = ray.p2();
318 QPointF h2 = ray.p2();
319 painter.drawLine(h1,h2);
328 QPointF selector_position;
332 selector_position = QPointF(
p->
sat*side,
p->
val*side);
338 qreal slice_h = side *
p->
val;
339 qreal ymin = side/2-slice_h/2;
341 selector_position = QPointF(
p->
val*height, ymin +
p->
sat*slice_h);
343 triangle.append(QPointF(0,side/2));
344 triangle.append(QPointF(height,0));
345 triangle.append(QPointF(height,side));
347 clip.addPolygon(triangle);
348 painter.setClipPath(clip);
352 painter.setClipping(
false);
355 painter.setPen(QPen(
p->
val > 0.5 ? Qt::black : Qt::white, 3));
356 painter.setBrush(Qt::NoBrush);
357 painter.drawEllipse(selector_position, selector_radius, selector_radius);
375 QLineF center_mouse_ln ( QPointF(0,0),
376 glob_mouse_ln.p2() - glob_mouse_ln.p1() );
388 QPointF pt = center_mouse_ln.p2();
392 qreal slice_h = side *
p->
val;
394 qreal ycenter = side/2;
395 qreal ymin = ycenter-slice_h/2;
398 p->
sat = qBound(0.0, (pt.y()-ymin)/slice_h, 1.0);
409 if ( ev->buttons() & Qt::LeftButton )
439 if (!qFuzzyCompare(oldh+1,
p->
hue+1))
447 p->
hue = qBound(0.0, h, 1.0);
454 p->
sat = qBound(0.0, s, 1.0);
460 p->
val = qBound(0.0, v, 1.0);
476 QColor old_col =
color();
479 p->
hue = old_col.hueF();
487 p->
hue = old_col.hueF();
495 p->
hue = old_col.hsvHueF();
496 p->
sat = old_col.hsvSaturationF();
497 p->
val = old_col.valueF();
523 default_flags = flags;
528 return default_flags & mask;
538 if ( event->mimeData()->hasColor() ||
539 (
event->mimeData()->hasText() && QColor(event->mimeData()->text()).isValid() ) )
540 event->acceptProposedAction();
545 if ( event->mimeData()->hasColor() )
547 setColor(event->mimeData()->colorData().value<QColor>());
550 else if ( event->mimeData()->hasText() )
552 QColor col(event->mimeData()->text());