diff options
| author | Aleix Pol <aleixpol@kde.org> | 2015-10-01 15:35:11 (GMT) |
|---|---|---|
| committer | Aleix Pol <aleixpol@kde.org> | 2015-10-01 15:35:11 (GMT) |
| commit | 1ccd6a94d57db36610caf8161c75a75cc5f1f9b4 (patch) | |
| tree | 29475fea3b2846d605ed86323fdbee9a0e547b31 | |
| parent | db728f94e39780d927eb565ca9a6c527811dd4a9 (diff) | |
Make the icon predominant color part of the grid background
| -rw-r--r-- | discover/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | discover/IconColors.cpp | 87 | ||||
| -rw-r--r-- | discover/IconColors.h | 46 | ||||
| -rw-r--r-- | discover/MuonDiscoverMainWindow.cpp | 2 | ||||
| -rw-r--r-- | discover/autotests/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | discover/autotests/IconColorsTest.cpp | 51 | ||||
| -rw-r--r-- | discover/qml/ApplicationsGridDelegate.qml | 12 |
7 files changed, 201 insertions, 1 deletions
diff --git a/discover/CMakeLists.txt b/discover/CMakeLists.txt index 4daa17a..02cd9c5 100644 --- a/discover/CMakeLists.txt +++ b/discover/CMakeLists.txt @@ -1,5 +1,6 @@ add_subdirectory(icons) add_subdirectory(muon-contenttheme) +add_subdirectory(autotests) include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/..) @@ -9,6 +10,7 @@ set(muon_discover_SRCS PaginateModel.cpp SystemFonts.cpp + IconColors.cpp ) qt5_add_resources(muon_discover_SRCS resources.qrc) diff --git a/discover/IconColors.cpp b/discover/IconColors.cpp new file mode 100644 index 0000000..e39682a --- /dev/null +++ b/discover/IconColors.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2015 Aleix Pol Gonzalez <aleixpol@blue-systems.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library/Lesser General Public License + * version 2, or (at your option) any later version, as published by the + * Free Software Foundation + * + * This program 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 Library/Lesser General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "IconColors.h" +#include <QIcon> +#include <vector> +#include <QDebug> + +// #define OUTPUT_PIXMAP_DEBUG + +IconColors::IconColors(QObject* parent) + : QObject(parent) +{} + +QString IconColors::iconName() const +{ + return m_iconName; +} + +void IconColors::setIconName(const QString& name) +{ + if (m_iconName != name) { + m_iconName = name; + } +} + +QColor IconColors::dominantColor() const +{ + const QImage img = QIcon::fromTheme(m_iconName).pixmap({32, 32}).toImage(); + const int tolerance = 10; + QVector<uint> hue(360/tolerance, 0); + +#ifdef OUTPUT_PIXMAP_DEBUG + QImage thing(img.size()+QSize(0,1), QImage::Format_ARGB32); + thing.fill(Qt::white); +#endif + + for (int w=0, cw=img.width(); w<cw; ++w) { + for (int h=0, ch=img.height(); h<ch; ++h) { + const QColor c = img.pixelColor(w, h); + + if (c.value()>150 && c.saturation()>20 && c.hue()>=0 && c.alpha()>200) { + hue[c.hue()/tolerance]++; + +#ifdef OUTPUT_PIXMAP_DEBUG +// qDebug() << "adopting" << w << "x" << h << c.name() << c.hue(); +// thing.setPixelColor(w, h, c); + thing.setPixelColor(w, h, QColor::fromHsv(tolerance*(c.hue()/tolerance), 220, 220)); +#endif + } + } + } + + uint dominantHue = 0, biggestAmount = 0; + for(int i=0; i<hue.size(); ++i) { + if (hue[i]>biggestAmount) { + biggestAmount = hue[i]; + dominantHue = i; + } + } + + QColor ret = QColor::fromHsv((dominantHue*tolerance + tolerance/2) % 360, 255, 255); + +#ifdef OUTPUT_PIXMAP_DEBUG + qDebug() << "dominant" << dominantHue << hue[dominantHue] << "~=" << ((100*hue[dominantHue])/(img.width()*img.height())) << "% " << m_iconName; + thing.setPixelColor(0, img.height(), ret); + thing.save("/tmp/"+m_iconName+".png"); +#endif + + return ret; +} diff --git a/discover/IconColors.h b/discover/IconColors.h new file mode 100644 index 0000000..2507299 --- /dev/null +++ b/discover/IconColors.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2015 Aleix Pol Gonzalez <aleixpol@blue-systems.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library/Lesser General Public License + * version 2, or (at your option) any later version, as published by the + * Free Software Foundation + * + * This program 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 Library/Lesser General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef ICONCOLORS_H +#define ICONCOLORS_H + +#include <QColor> +#include <QObject> + +class IconColors : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString iconName READ iconName WRITE setIconName) + Q_PROPERTY(QColor dominantColor READ dominantColor NOTIFY dominantColorChanged STORED false) +public: + IconColors(QObject* parent = Q_NULLPTR); + + QString iconName() const; + void setIconName(const QString& name); + + QColor dominantColor() const; + +Q_SIGNALS: + void dominantColorChanged(const QColor &dominantColor); + +private: + QString m_iconName; +}; + +#endif // ICONCOLORS_H diff --git a/discover/MuonDiscoverMainWindow.cpp b/discover/MuonDiscoverMainWindow.cpp index fbfcf58..3aed283 100644 --- a/discover/MuonDiscoverMainWindow.cpp +++ b/discover/MuonDiscoverMainWindow.cpp @@ -20,6 +20,7 @@ #include "MuonDiscoverMainWindow.h" #include "PaginateModel.h" #include "SystemFonts.h" +#include "IconColors.h" // Qt includes #include <QDebug> @@ -77,6 +78,7 @@ MuonDiscoverMainWindow::MuonDiscoverMainWindow() kdeclarative.setupBindings(); qmlRegisterType<PaginateModel>("org.kde.muon.discover", 1, 0, "PaginateModel"); + qmlRegisterType<IconColors>("org.kde.muon.discover", 1, 0, "IconColors"); qmlRegisterSingletonType<SystemFonts>("org.kde.muon.discover", 1, 0, "SystemFonts", ([](QQmlEngine*, QJSEngine*) -> QObject* { return new SystemFonts; })); qmlRegisterType<KXmlGuiWindow>(); qmlRegisterType<QActionGroup>(); diff --git a/discover/autotests/CMakeLists.txt b/discover/autotests/CMakeLists.txt new file mode 100644 index 0000000..b70c4c0 --- /dev/null +++ b/discover/autotests/CMakeLists.txt @@ -0,0 +1,2 @@ +add_executable(IconColorsTest IconColorsTest.cpp ../IconColors.cpp) +target_link_libraries(IconColorsTest Qt5::Test Qt5::Gui) diff --git a/discover/autotests/IconColorsTest.cpp b/discover/autotests/IconColorsTest.cpp new file mode 100644 index 0000000..3578c0a --- /dev/null +++ b/discover/autotests/IconColorsTest.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2015 Aleix Pol Gonzalez <aleixpol@blue-systems.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library/Lesser General Public License + * version 2, or (at your option) any later version, as published by the + * Free Software Foundation + * + * This program 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 Library/Lesser General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include <QtTest> +#include "../IconColors.h" + +class IconColorsTest : public QObject +{ + Q_OBJECT +public: + IconColorsTest() {} + +private slots: + void testIcon_data() { + QTest::addColumn<QString>("iconName"); + QTest::addColumn<int>("hue"); + + QTest::newRow("akregator") << "akregator" << 15; + QTest::newRow("korganizer") << "korganizer" << 105; + } + + void testIcon() { + QFETCH(QString, iconName); + QFETCH(int, hue); + + IconColors colors; + colors.setIconName(iconName); + + QCOMPARE(colors.dominantColor().hue(), hue); + } +}; + +QTEST_MAIN( IconColorsTest ) + +#include "IconColorsTest.moc" diff --git a/discover/qml/ApplicationsGridDelegate.qml b/discover/qml/ApplicationsGridDelegate.qml index bd26a70..de5641a 100644 --- a/discover/qml/ApplicationsGridDelegate.qml +++ b/discover/qml/ApplicationsGridDelegate.qml @@ -21,6 +21,7 @@ import QtQuick 2.1 import QtQuick.Layouts 1.1 import QtQuick.Controls 1.1 import org.kde.kquickcontrolsaddons 2.0 +import org.kde.muon.discover 1.0 import "navigation.js" as Navigation GridItem { @@ -36,11 +37,20 @@ GridItem { SystemPalette { id: sys } Rectangle { id: artwork - color: sys.shadow + gradient: Gradient { + GradientStop { position: 0.0; color: Qt.darker(colors.dominantColor) } + GradientStop { position: 0.5; color: "black" } + } + width: parent.width height: parent.height*0.65 property bool hasThumbnail: model.application.thumbnailUrl!="" + IconColors { + id: colors + iconName: model.application.icon + } + Image { id: screen anchors { |
