Back to index

unity  6.0.0
HudIconTextureSource.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2011 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Gordon Allott <gord.allott@canonical.com>
00018  */
00019 
00020 
00021 #include "HudIconTextureSource.h"
00022 #include "config.h"
00023 
00024 #include <glib.h>
00025 
00026 #include <Nux/Nux.h>
00027 #include <NuxCore/Logger.h>
00028 
00029 namespace
00030 {
00031   nux::logging::Logger logger("unity.hud.HudIconTextureSource");
00032 }
00033 
00034 namespace unity
00035 {
00036 namespace hud
00037 {
00038 
00039 HudIconTextureSource::HudIconTextureSource(nux::ObjectPtr<nux::BaseTexture> texture)
00040   : unity::ui::IconTextureSource()
00041   , icon_texture_(texture)
00042 {
00043 }
00044 
00045 HudIconTextureSource::~HudIconTextureSource()
00046 {
00047 }
00048 
00049 void HudIconTextureSource::ColorForIcon(GdkPixbuf* pixbuf)
00050 {
00051   if (GDK_IS_PIXBUF(pixbuf))
00052   {
00053     unsigned int width = gdk_pixbuf_get_width(pixbuf);
00054     unsigned int height = gdk_pixbuf_get_height(pixbuf);
00055     unsigned int row_bytes = gdk_pixbuf_get_rowstride(pixbuf);
00056     
00057     long int rtotal = 0, gtotal = 0, btotal = 0;
00058     float total = 0.0f;
00059     
00060     guchar* img = gdk_pixbuf_get_pixels(pixbuf);
00061     
00062     for (unsigned int i = 0; i < width; i++)
00063     {
00064       for (unsigned int j = 0; j < height; j++)
00065       {
00066         guchar* pixels = img + (j * row_bytes + i * 4);
00067         guchar r = *(pixels + 0);
00068         guchar g = *(pixels + 1);
00069         guchar b = *(pixels + 2);
00070         guchar a = *(pixels + 3);
00071         
00072         float saturation = (MAX(r, MAX(g, b)) - MIN(r, MIN(g, b))) / 255.0f;
00073         float relevance = .1 + .9 * (a / 255.0f) * saturation;
00074         
00075         rtotal += (guchar)(r * relevance);
00076         gtotal += (guchar)(g * relevance);
00077         btotal += (guchar)(b * relevance);
00078         
00079         total += relevance * 255;
00080       }
00081     }
00082     
00083     nux::color::RedGreenBlue rgb(rtotal / total,
00084                                  gtotal / total,
00085                                  btotal / total);
00086     nux::color::HueSaturationValue hsv(rgb);
00087     
00088     if (hsv.saturation > 0.15f)
00089       hsv.saturation = 0.65f;
00090     
00091     hsv.value = 0.90f;
00092     bg_color = nux::Color(nux::color::RedGreenBlue(hsv));
00093   }
00094   else
00095   {
00096     LOG_ERROR(logger) << "Pixbuf (" << pixbuf << ") passed is non valid";
00097     bg_color = nux::Color(255,255,255,255);
00098   }
00099 }
00100 
00101 nux::Color HudIconTextureSource::BackgroundColor() const
00102 {
00103   return bg_color;
00104 }
00105 
00106 nux::BaseTexture* HudIconTextureSource::TextureForSize(int size)
00107 {
00108   return icon_texture_.GetPointer();
00109 }
00110 
00111 nux::Color HudIconTextureSource::GlowColor()
00112 {
00113  return nux::Color(0.0f, 0.0f, 0.0f, 0.0f); 
00114 }
00115 
00116 nux::BaseTexture* HudIconTextureSource::Emblem()
00117 {
00118   return nullptr;
00119 }
00120 
00121 }
00122 }
00123