HuePanel.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "HuePanel.h"
  2. #include <QMouseEvent>
  3. #include <QPainter>
  4. HuePanel::HuePanel(const QColor &c, QWidget *parent) : QWidget(parent)
  5. {
  6. setFixedSize(256, 256);
  7. huePixmap = QPixmap(":/qfluentwidgets/images/color_dialog/HuePanel.png");
  8. setColor(c);
  9. }
  10. void HuePanel::setColor(const QColor &c)
  11. {
  12. color = c;
  13. color.setHsv(color.hue(), color.saturation(), 255);
  14. pickerPos = QPoint(hue() * width() / 360, (255 - saturation()) * height() / 255);
  15. update();
  16. }
  17. int HuePanel::hue() const
  18. {
  19. return color.hue();
  20. }
  21. int HuePanel::saturation() const
  22. {
  23. return color.saturation();
  24. }
  25. void HuePanel::setPickerPosition(const QPoint &pos)
  26. {
  27. pickerPos = pos;
  28. color.setHsv(qMax(0.0, qMin(1.0, pos.x() * 1.0 / width())) * 360,
  29. qMax(0.0, qMin(1.0, (height() - pos.y()) * 1.0 / height()) * 255), 255);
  30. update();
  31. emit colorChanged(color);
  32. }
  33. void HuePanel::mousePressEvent(QMouseEvent *event)
  34. {
  35. setPickerPosition(event->pos());
  36. }
  37. void HuePanel::mouseMoveEvent(QMouseEvent *event)
  38. {
  39. setPickerPosition(event->pos());
  40. }
  41. void HuePanel::paintEvent(QPaintEvent * /*event*/)
  42. {
  43. QPainter painter(this);
  44. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  45. // draw hue panel
  46. painter.setBrush(QBrush(huePixmap));
  47. painter.setPen(QPen(QBrush(QColor(0, 0, 0, 15)), 2.4));
  48. painter.drawRoundedRect(this->rect(), 5.6, 5.6);
  49. // draw picker
  50. if (saturation() > 153 || hue() < 180) {
  51. color = QColor(Qt::black);
  52. } else {
  53. color = QColor(255, 253, 254);
  54. }
  55. painter.setPen(QPen(color, 3));
  56. painter.setBrush(Qt::NoBrush);
  57. painter.drawEllipse(pickerPos.x() - 8, pickerPos.y() - 8, 16, 16);
  58. }