Exemplo n.º 1
0
void Plot3DDialog::setColorMapPreview(const QString& fileName)
{
    if (fileName.isEmpty()) {
        colorMapPreviewLabel->setText(tr("None"));
        return;
    }

    ColorVector cv;
    if (!Graph3D::openColorMapFile(cv, fileName)) {
        colorMapPreviewLabel->setText(tr("None"));
        return;
    }

    int height = 20;
    QPixmap pix;
    pix.resize(cv.size(), height);
    QPainter p(&pix);
    for (unsigned i = 0; i != cv.size(); ++i) {
        RGBA rgb = cv[i];
        p.setPen(GL2Qt(rgb.r, rgb.g, rgb.b));
        p.drawLine(QPoint(0, 0), QPoint(0, height));
        p.translate(1, 0);
    }
    p.end();
    colorMapPreviewLabel->setPixmap(pix);
}
Exemplo n.º 2
0
void MoodbarRenderer::Render(const ColorVector& colors, QPainter* p,
                             const QRect& rect) {
  // Sample the colors and map them to screen pixels.
  ColorVector screen_colors;
  for (int x = 0; x < rect.width(); ++x) {
    int r = 0;
    int g = 0;
    int b = 0;

    int start = x * colors.size() / rect.width();
    int end = (x + 1) * colors.size() / rect.width();

    if (start == end) end = qMin(start + 1, colors.size() - 1);

    for (int j = start; j < end; j++) {
      r += colors[j].red();
      g += colors[j].green();
      b += colors[j].blue();
    }

    const int n = qMax(1, end - start);
    screen_colors.append(QColor(r / n, g / n, b / n));
  }

  // Draw the actual moodbar.
  for (int x = 0; x < rect.width(); x++) {
    int h, s, v;
    screen_colors[x].getHsv(&h, &s, &v);

    for (int y = 0; y <= rect.height() / 2; y++) {
      float coeff = float(y) / float(rect.height() / 2);
      float coeff2 = 1.0f - ((1.0f - coeff) * (1.0f - coeff));
      coeff = 1.0f - (1.0f - coeff) / 2.0f;
      coeff2 = 1.f - (1.f - coeff2) / 2.0f;

      p->setPen(QColor::fromHsv(
          h, qBound(0, int(float(s) * coeff), 255),
          qBound(0, int(255.f - (255.f - float(v)) * coeff2), 255)));

      p->drawPoint(rect.left() + x, rect.top() + y);
      p->drawPoint(rect.left() + x, rect.top() + rect.height() - 1 - y);
    }
  }
}