1
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
3
* Copyright (C) 2011 Canonical Ltd
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 3 as
7
* published by the Free Software Foundation.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Authored by: Gordon Allott <gord.allott@canonical.com>
21
#include "HudIconTextureSource.h"
27
#include <NuxCore/Logger.h>
31
nux::logging::Logger logger("unity.hud.HudIconTextureSource");
39
HudIconTextureSource::HudIconTextureSource(nux::ObjectPtr<nux::BaseTexture> texture)
40
: unity::ui::IconTextureSource()
41
, icon_texture_(texture)
45
HudIconTextureSource::~HudIconTextureSource()
49
void HudIconTextureSource::ColorForIcon(GdkPixbuf* pixbuf)
51
if (GDK_IS_PIXBUF(pixbuf))
53
unsigned int width = gdk_pixbuf_get_width(pixbuf);
54
unsigned int height = gdk_pixbuf_get_height(pixbuf);
55
unsigned int row_bytes = gdk_pixbuf_get_rowstride(pixbuf);
57
long int rtotal = 0, gtotal = 0, btotal = 0;
60
guchar* img = gdk_pixbuf_get_pixels(pixbuf);
62
for (unsigned int i = 0; i < width; i++)
64
for (unsigned int j = 0; j < height; j++)
66
guchar* pixels = img + (j * row_bytes + i * 4);
67
guchar r = *(pixels + 0);
68
guchar g = *(pixels + 1);
69
guchar b = *(pixels + 2);
70
guchar a = *(pixels + 3);
72
float saturation = (MAX(r, MAX(g, b)) - MIN(r, MIN(g, b))) / 255.0f;
73
float relevance = .1 + .9 * (a / 255.0f) * saturation;
75
rtotal += (guchar)(r * relevance);
76
gtotal += (guchar)(g * relevance);
77
btotal += (guchar)(b * relevance);
79
total += relevance * 255;
83
nux::color::RedGreenBlue rgb(rtotal / total,
86
nux::color::HueSaturationValue hsv(rgb);
88
if (hsv.saturation > 0.15f)
89
hsv.saturation = 0.65f;
92
bg_color = nux::Color(nux::color::RedGreenBlue(hsv));
96
LOG_ERROR(logger) << "Pixbuf (" << pixbuf << ") passed is non valid";
97
bg_color = nux::Color(255,255,255,255);
101
nux::Color HudIconTextureSource::BackgroundColor()
106
nux::BaseTexture* HudIconTextureSource::TextureForSize(int size)
108
return icon_texture_.GetPointer();
111
nux::Color HudIconTextureSource::GlowColor()
113
return nux::Color(0.0f, 0.0f, 0.0f, 0.0f);
116
nux::BaseTexture* HudIconTextureSource::Emblem()