2012-05-26 00:56:55 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-05-26 00:56:55 +02:00
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-05-26 00:56:55 +02:00
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-05-26 00:56:55 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "moodbarrenderer.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPalette>
|
|
|
|
|
2014-04-29 14:11:52 +02:00
|
|
|
#include "core/arraysize.h"
|
|
|
|
|
2012-05-26 00:56:55 +02:00
|
|
|
const int MoodbarRenderer::kNumHues = 12;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
ColorVector MoodbarRenderer::Colors(const QByteArray& data, MoodbarStyle style,
|
|
|
|
const QPalette& palette) {
|
2012-05-26 00:56:55 +02:00
|
|
|
const int samples = data.size() / 3;
|
|
|
|
|
|
|
|
// Set some parameters based on the moodbar style
|
|
|
|
StyleProperties properties;
|
2014-02-07 16:34:20 +01:00
|
|
|
switch (style) {
|
|
|
|
case Style_Angry:
|
|
|
|
properties = StyleProperties(samples / 360 * 9, 45, -45, 200, 100);
|
|
|
|
break;
|
|
|
|
case Style_Frozen:
|
|
|
|
properties = StyleProperties(samples / 360 * 1, 140, 160, 50, 100);
|
|
|
|
break;
|
|
|
|
case Style_Happy:
|
|
|
|
properties = StyleProperties(samples / 360 * 2, 0, 359, 150, 250);
|
|
|
|
break;
|
|
|
|
case Style_Normal:
|
|
|
|
properties = StyleProperties(samples / 360 * 3, 0, 359, 100, 100);
|
|
|
|
break;
|
2012-05-28 00:44:49 +02:00
|
|
|
case Style_SystemPalette:
|
2012-05-26 00:56:55 +02:00
|
|
|
default: {
|
2014-02-07 16:34:20 +01:00
|
|
|
const QColor highlight_color(
|
|
|
|
palette.color(QPalette::Active, QPalette::Highlight));
|
2012-05-26 00:56:55 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
properties.threshold_ = samples / 360 * 3;
|
2012-05-26 00:56:55 +02:00
|
|
|
properties.range_start_ = (highlight_color.hsvHue() - 20 + 360) % 360;
|
|
|
|
properties.range_delta_ = 20;
|
2014-02-07 16:34:20 +01:00
|
|
|
properties.sat_ = highlight_color.hsvSaturation();
|
|
|
|
properties.val_ = highlight_color.value() / 2;
|
2012-05-26 00:56:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char* data_p =
|
|
|
|
reinterpret_cast<const unsigned char*>(data.constData());
|
|
|
|
|
|
|
|
int hue_distribution[360];
|
|
|
|
int total = 0;
|
|
|
|
|
2014-05-23 13:31:50 +02:00
|
|
|
memset(hue_distribution, 0, sizeof(hue_distribution));
|
2012-05-26 00:56:55 +02:00
|
|
|
|
2012-05-27 17:46:16 +02:00
|
|
|
ColorVector colors;
|
2012-05-26 00:56:55 +02:00
|
|
|
|
|
|
|
// Read the colors, keeping track of some histograms
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < samples; ++i) {
|
2012-05-26 00:56:55 +02:00
|
|
|
QColor color;
|
|
|
|
color.setRed(int(*data_p++));
|
|
|
|
color.setGreen(int(*data_p++));
|
|
|
|
color.setBlue(int(*data_p++));
|
|
|
|
|
|
|
|
colors << color;
|
|
|
|
|
|
|
|
const int hue = qMax(0, color.hue());
|
|
|
|
if (hue_distribution[hue]++ == properties.threshold_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
total++;
|
2012-05-26 00:56:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:19:11 +02:00
|
|
|
total = qMax(total, 1);
|
|
|
|
|
2012-05-26 00:56:55 +02:00
|
|
|
// Remap the hue values to be between rangeStart and
|
|
|
|
// rangeStart + rangeDelta. Every time we see an input hue
|
|
|
|
// above the threshold, increment the output hue by
|
|
|
|
// (1/total) * rangeDelta.
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0, n = 0; i < 360; i++) {
|
2012-05-26 00:56:55 +02:00
|
|
|
hue_distribution[i] =
|
2014-02-07 16:34:20 +01:00
|
|
|
((hue_distribution[i] > properties.threshold_ ? n++ : n) *
|
|
|
|
properties.range_delta_ / total +
|
|
|
|
properties.range_start_) %
|
|
|
|
360;
|
2012-05-26 00:56:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now huedist is a hue mapper: huedist[h] is the new hue value
|
|
|
|
// for a bar with hue h
|
2014-02-07 16:34:20 +01:00
|
|
|
for (ColorVector::iterator it = colors.begin(); it != colors.end(); ++it) {
|
2012-05-26 00:56:55 +02:00
|
|
|
const int hue = qMax(0, it->hue());
|
|
|
|
|
|
|
|
*it = QColor::fromHsv(
|
2014-02-07 16:34:20 +01:00
|
|
|
qBound(0, hue_distribution[hue], 359),
|
|
|
|
qBound(0, it->saturation() * properties.sat_ / 100, 255),
|
|
|
|
qBound(0, it->value() * properties.val_ / 100, 255));
|
2012-05-26 00:56:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return colors;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void MoodbarRenderer::Render(const ColorVector& colors, QPainter* p,
|
|
|
|
const QRect& rect) {
|
2012-05-26 00:56:55 +02:00
|
|
|
// Sample the colors and map them to screen pixels.
|
2012-05-27 17:46:16 +02:00
|
|
|
ColorVector screen_colors;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int x = 0; x < rect.width(); ++x) {
|
2012-05-26 00:56:55 +02:00
|
|
|
int r = 0;
|
|
|
|
int g = 0;
|
|
|
|
int b = 0;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int start = x * colors.size() / rect.width();
|
|
|
|
int end = (x + 1) * colors.size() / rect.width();
|
2012-05-26 00:56:55 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (start == end) end = qMin(start + 1, colors.size() - 1);
|
2012-05-26 00:56:55 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int j = start; j < end; j++) {
|
2012-05-26 00:56:55 +02:00
|
|
|
r += colors[j].red();
|
|
|
|
g += colors[j].green();
|
|
|
|
b += colors[j].blue();
|
|
|
|
}
|
|
|
|
|
2013-08-24 21:07:44 +02:00
|
|
|
const int n = qMax(1, end - start);
|
2014-02-07 16:34:20 +01:00
|
|
|
screen_colors.append(QColor(r / n, g / n, b / n));
|
2012-05-26 00:56:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the actual moodbar.
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int x = 0; x < rect.width(); x++) {
|
2012-05-26 00:56:55 +02:00
|
|
|
int h, s, v;
|
2014-02-07 16:34:20 +01:00
|
|
|
screen_colors[x].getHsv(&h, &s, &v);
|
2012-05-26 00:56:55 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int y = 0; y <= rect.height() / 2; y++) {
|
|
|
|
float coeff = float(y) / float(rect.height() / 2);
|
2012-05-26 00:56:55 +02:00
|
|
|
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(
|
2014-02-07 16:34:20 +01:00
|
|
|
h, qBound(0, int(float(s) * coeff), 255),
|
2012-05-26 00:56:55 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-27 19:53:57 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QImage MoodbarRenderer::RenderToImage(const ColorVector& colors,
|
|
|
|
const QSize& size) {
|
2012-05-27 19:53:57 +02:00
|
|
|
QImage image(size, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
QPainter p(&image);
|
|
|
|
Render(colors, &p, image.rect());
|
|
|
|
p.end();
|
|
|
|
return image;
|
|
|
|
}
|
2012-05-28 13:50:07 +02:00
|
|
|
|
|
|
|
QString MoodbarRenderer::StyleName(MoodbarStyle style) {
|
|
|
|
switch (style) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case Style_Normal:
|
|
|
|
return QObject::tr("Normal");
|
|
|
|
case Style_Angry:
|
|
|
|
return QObject::tr("Angry");
|
|
|
|
case Style_Frozen:
|
|
|
|
return QObject::tr("Frozen");
|
|
|
|
case Style_Happy:
|
|
|
|
return QObject::tr("Happy");
|
|
|
|
case Style_SystemPalette:
|
|
|
|
return QObject::tr("System colors");
|
2012-05-28 13:50:07 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
default:
|
|
|
|
return QString();
|
2012-05-28 13:50:07 +02:00
|
|
|
}
|
|
|
|
}
|