/* This file is part of Clementine. Copyright 2012, David Sansome 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. 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. You should have received a copy of the GNU General Public License along with Clementine. If not, see . */ #include "moodbarrenderer.h" #include #include const int MoodbarRenderer::kNumHues = 12; ColorVector MoodbarRenderer::Colors( const QByteArray& data, MoodbarStyle style, const QPalette& palette) { const int samples = data.size() / 3; // Set some parameters based on the moodbar style StyleProperties properties; 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; case Style_SystemPalette: default: { const QColor highlight_color(palette.color(QPalette::Active, QPalette::Highlight)); properties.threshold_ = samples / 360 * 3; properties.range_start_ = (highlight_color.hsvHue() - 20 + 360) % 360; properties.range_delta_ = 20; properties.sat_ = highlight_color.hsvSaturation(); properties.val_ = highlight_color.value() / 2; } } const unsigned char* data_p = reinterpret_cast(data.constData()); int hue_distribution[360]; int total = 0; memset(hue_distribution, 0, sizeof(hue_distribution)); ColorVector colors; // Read the colors, keeping track of some histograms for (int i=0; i properties.threshold_ ? n++ : n ) * properties.range_delta_ / total + properties.range_start_) % 360; } // Now huedist is a hue mapper: huedist[h] is the new hue value // for a bar with hue h for (ColorVector::iterator it = colors.begin() ; it != colors.end() ; ++it) { const int hue = qMax(0, it->hue()); *it = QColor::fromHsv( qBound(0, hue_distribution[hue], 359), qBound(0, it->saturation() * properties.sat_ / 100, 255), qBound(0, it->value() * properties.val_ / 100, 255)); } return colors; } 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; xsetPen(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); } } } QImage MoodbarRenderer::RenderToImage(const ColorVector& colors, const QSize& size) { QImage image(size, QImage::Format_ARGB32_Premultiplied); QPainter p(&image); Render(colors, &p, image.rect()); p.end(); return image; } QString MoodbarRenderer::StyleName(MoodbarStyle style) { switch (style) { 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"); default: return QString(); } }