Newer
Older
Import / applications / MakePDF / ToneGradient.cpp
#include <QWidget>
#include <QPainter>
#include <QSignalMapper>
#include <QMouseEvent>
#include "ToneGradient.h"


extern uint32_t rgbFromHsvF(qreal h1, qreal s, qreal v, qreal /*a*/);
static const int dotSiz = 18;// 16;
static const int dotAlpha = 96;// 64;


ToneGradient::ToneGradient(QWidget* parent) : QWidget(parent)
{
  m_palette.m_mode = 0;
  m_palette.m_primaryHue = 0;
  m_palette.m_secondaryHueDelta = 10;
  m_palette.m_saturation = 255;
  m_palette.m_stretch = 31;
}


void ToneGradient::updatePalette(ColorScheme a_scheme)
{
  m_palette.m_mode = a_scheme.m_mode;
  m_palette.m_primaryHue = a_scheme.m_primaryHue;
  m_palette.m_secondaryHueDelta = a_scheme.m_secondaryHueDelta;
  update();
}


void ToneGradient::updateShades(ColorScheme a_scheme)
{
  m_palette.m_saturation = a_scheme.m_saturation;
  m_palette.m_stretch = a_scheme.m_stretch;
  update();
}


void ToneGradient::mousePressEvent(QMouseEvent* me)
{
  mouseMoveEvent(me);
}


void ToneGradient::mouseReleaseEvent(QMouseEvent* me)
{
  mouseMoveEvent(me);
}


void ToneGradient::mouseMoveEvent(QMouseEvent* me)
{
  int x = me->pos().x();
  int y = me->pos().y();
  if (x < 0) x = 0;
  if (x >= width()) x = width() - 1;
  if (y < 0) y = 0;
  if (y >= height()) y = height() - 1;
  m_palette.m_stretch = (x * 32) / width();
  m_palette.m_saturation = (y * 256) / height();
  paletteChanged();
  update();
}


void ToneGradient::paintEvent(QPaintEvent*)
{
    QPainter p(this);

    // Draw the tone gradient
    int w = width();
    int h = height();
    float hue = m_palette.hue(0); // Get the primary color
    QImage img(w, h, QImage::Format_ARGB32);
    uint32_t* bits = (uint32_t*)img.bits();
    int pitch = img.bytesPerLine() / sizeof(uint32_t);
    for (int j = 0; j < h; j++)
    {
        for (int i = 0; i < w; i++)
        {
            bits[j*pitch + i] = rgbFromHsvF(hue, j / float(h), i / float(w), 1.0);
        }
    }
    p.drawImage(0, 0, img);

    int x = (m_palette.m_stretch * width()) / 32;
    int y = (m_palette.m_saturation * height()) / 256;
    p.setPen(QPen(QColor(255, 255, 255, dotAlpha + 20), 2));
    p.setBrush(QColor(192, 192, 192, dotAlpha + 20));
    p.drawEllipse(x - dotSiz / 2, y - dotSiz / 2, dotSiz, dotSiz);
}