ColorGradient.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #include <QWidget>
  3. #include <QColor>
  4. #include <QComboBox>
  5. #include <list>
  6. #include <QLineEdit>
  7. #include "ColorButton.h"
  8. struct ColorPoint {
  9. double alpha;
  10. QColor color;
  11. ColorPoint(double alpha, QColor color) : alpha(std::clamp(alpha, 0.0, 1.0)), color(color) {}
  12. };
  13. class ColorGradient : public QWidget
  14. {
  15. Q_OBJECT
  16. public:
  17. enum ColorInterpolationMode{ RGB, HSL};
  18. enum ColorMode{ Interpolate, Discrete};
  19. ColorGradient(QWidget *parent);
  20. ~ColorGradient();
  21. QColor getColorFromAlpha(double alpha);
  22. QColor getColorFromValue(double value);
  23. void addColorPoint(double alpha);
  24. void removeColorPoint(ColorPoint& point);
  25. void setColorPointAlpha(ColorPoint& point, double newAlpha);
  26. void setColorPointColor(ColorPoint& point, QColor newColor);
  27. void setMode(ColorMode mode);
  28. void setInterpolationMode(ColorInterpolationMode mode);
  29. void setMinValue(double value);
  30. void setMaxValue(double value);
  31. private:
  32. double minValue = 0, maxValue = 100;
  33. ColorMode mode = ColorMode::Interpolate;
  34. ColorInterpolationMode interpolationMode = ColorInterpolationMode::RGB;
  35. std::list<ColorPoint> pointList;
  36. //UI
  37. QLineEdit* minEdit;
  38. QLineEdit* maxEdit;
  39. ColorPoint* selected = nullptr;
  40. QComboBox* interpolationCombobox = nullptr;
  41. QComboBox* modeCombobox = nullptr;
  42. QSpacerItem* spacerH = nullptr;
  43. QLineEdit* alphaLineEdit = nullptr;
  44. ColorButton* colorButton = nullptr;
  45. QLineEdit* valueEdit;
  46. QLabel* colorLabel;
  47. QLabel* alphaLabel;
  48. QLabel* valueLabel;
  49. void hideSelectionEdit(bool value);
  50. void setAlphaOfSelected();
  51. void setValueOfSelected();
  52. void selectColorPoint(ColorPoint* point);
  53. void paintEvent(QPaintEvent* event) override;
  54. void mousePressEvent(QMouseEvent* event) override;
  55. void mouseMoveEvent(QMouseEvent* event) override;
  56. void minEditChangeEvent();
  57. void maxEditChangeEvent();
  58. signals:
  59. void gradientChanged();
  60. };