~azzar1/unity/proper-reboot

« back to all changes in this revision

Viewing changes to plugins/unityshell/src/HudIconTextureSource.cpp

  • Committer: Daniel van Vugt
  • Date: 2012-03-14 06:24:18 UTC
  • mfrom: (2108 unity)
  • mto: This revision was merged to the branch mainline in revision 2146.
  • Revision ID: daniel.van.vugt@canonical.com-20120314062418-nprucpbr0m7qky5e
MergedĀ latestĀ lp:unity

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
 
2
/*
 
3
 * Copyright (C) 2011 Canonical Ltd
 
4
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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/>.
 
16
 *
 
17
 * Authored by: Gordon Allott <gord.allott@canonical.com>
 
18
 */
 
19
 
 
20
 
 
21
#include "HudIconTextureSource.h"
 
22
#include "config.h"
 
23
 
 
24
#include <glib.h>
 
25
 
 
26
#include <Nux/Nux.h>
 
27
#include <NuxCore/Logger.h>
 
28
 
 
29
namespace
 
30
{
 
31
  nux::logging::Logger logger("unity.hud.HudIconTextureSource");
 
32
}
 
33
 
 
34
namespace unity
 
35
{
 
36
namespace hud
 
37
{
 
38
 
 
39
HudIconTextureSource::HudIconTextureSource(nux::ObjectPtr<nux::BaseTexture> texture)
 
40
  : unity::ui::IconTextureSource()
 
41
  , icon_texture_(texture)
 
42
{
 
43
}
 
44
 
 
45
HudIconTextureSource::~HudIconTextureSource()
 
46
{
 
47
}
 
48
 
 
49
void HudIconTextureSource::ColorForIcon(GdkPixbuf* pixbuf)
 
50
{
 
51
  if (GDK_IS_PIXBUF(pixbuf))
 
52
  {
 
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);
 
56
    
 
57
    long int rtotal = 0, gtotal = 0, btotal = 0;
 
58
    float total = 0.0f;
 
59
    
 
60
    guchar* img = gdk_pixbuf_get_pixels(pixbuf);
 
61
    
 
62
    for (unsigned int i = 0; i < width; i++)
 
63
    {
 
64
      for (unsigned int j = 0; j < height; j++)
 
65
      {
 
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);
 
71
        
 
72
        float saturation = (MAX(r, MAX(g, b)) - MIN(r, MIN(g, b))) / 255.0f;
 
73
        float relevance = .1 + .9 * (a / 255.0f) * saturation;
 
74
        
 
75
        rtotal += (guchar)(r * relevance);
 
76
        gtotal += (guchar)(g * relevance);
 
77
        btotal += (guchar)(b * relevance);
 
78
        
 
79
        total += relevance * 255;
 
80
      }
 
81
    }
 
82
    
 
83
    nux::color::RedGreenBlue rgb(rtotal / total,
 
84
                                 gtotal / total,
 
85
                                 btotal / total);
 
86
    nux::color::HueSaturationValue hsv(rgb);
 
87
    
 
88
    if (hsv.saturation > 0.15f)
 
89
      hsv.saturation = 0.65f;
 
90
    
 
91
    hsv.value = 0.90f;
 
92
    bg_color = nux::Color(nux::color::RedGreenBlue(hsv));
 
93
  }
 
94
  else
 
95
  {
 
96
    LOG_ERROR(logger) << "Pixbuf (" << pixbuf << ") passed is non valid";
 
97
    bg_color = nux::Color(255,255,255,255);
 
98
  }
 
99
}
 
100
 
 
101
nux::Color HudIconTextureSource::BackgroundColor()
 
102
{
 
103
  return bg_color;
 
104
}
 
105
 
 
106
nux::BaseTexture* HudIconTextureSource::TextureForSize(int size)
 
107
{
 
108
  return icon_texture_.GetPointer();
 
109
}
 
110
 
 
111
nux::Color HudIconTextureSource::GlowColor()
 
112
{
 
113
 return nux::Color(0.0f, 0.0f, 0.0f, 0.0f); 
 
114
}
 
115
 
 
116
nux::BaseTexture* HudIconTextureSource::Emblem()
 
117
{
 
118
  return nullptr;
 
119
}
 
120
 
 
121
}
 
122
}
 
123