~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/web/lime/lib/Color.js

  • Committer: parra
  • Date: 2010-03-15 15:56:56 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1448
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This notice must be untouched at all times.
 
2
 
 
3
Open-jACOB Draw2D
 
4
The latest version is available at
 
5
http://www.openjacob.org
 
6
 
 
7
Copyright (c) 2006 Andreas Herz. All rights reserved.
 
8
Created 5. 11. 2006 by Andreas Herz (Web: http://www.freegroup.de )
 
9
 
 
10
LICENSE: LGPL
 
11
 
 
12
This library is free software; you can redistribute it and/or
 
13
modify it under the terms of the GNU Lesser General Public
 
14
License (LGPL) as published by the Free Software Foundation; either
 
15
version 2.1 of the License, or (at your option) any later version.
 
16
 
 
17
This library is distributed in the hope that it will be useful,
 
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
20
Lesser General Public License for more details.
 
21
 
 
22
You should have received a copy of the GNU Lesser General Public
 
23
License along with this library; if not, write to the Free Software
 
24
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA,
 
25
or see http://www.gnu.org/copyleft/lesser.html
 
26
*/
 
27
 
 
28
/**
 
29
 * 
 
30
 * @version 0.9.14
 
31
 * @author Andreas Herz
 
32
 * @constructor
 
33
 */
 
34
draw2d.Color=function(/*:int*/ red,/*:int*/ green ,/*:int*/ blue)
 
35
{
 
36
  if(typeof green == "undefined")
 
37
  {
 
38
    var rgb = this.hex2rgb(red);
 
39
    /** @private **/
 
40
    this.red= rgb[0];
 
41
    /** @private **/
 
42
    this.green = rgb[1];
 
43
    /** @private **/
 
44
    this.blue = rgb[2];
 
45
  }
 
46
  else
 
47
  {
 
48
    /** @private **/
 
49
    this.red= red;
 
50
    /** @private **/
 
51
    this.green = green;
 
52
    /** @private **/
 
53
    this.blue = blue;
 
54
  }
 
55
}
 
56
/** @private **/
 
57
draw2d.Color.prototype.type="Color";
 
58
 
 
59
/**
 
60
 * @private
 
61
 **/
 
62
draw2d.Color.prototype.getHTMLStyle=function()
 
63
{
 
64
  return "rgb("+this.red+","+this.green+","+this.blue+")";
 
65
}
 
66
 
 
67
/**
 
68
 * Return the [red] part of the color.
 
69
 * @type int
 
70
 **/
 
71
draw2d.Color.prototype.getRed=function()
 
72
{
 
73
  return this.red;
 
74
}
 
75
 
 
76
 
 
77
/**
 
78
 * Return the [green] part of the color.
 
79
 * @type int
 
80
 **/
 
81
draw2d.Color.prototype.getGreen=function()
 
82
{
 
83
  return this.green;
 
84
}
 
85
 
 
86
 
 
87
/**
 
88
 * Return the [blue] part of the color.
 
89
 * @type int
 
90
 **/
 
91
draw2d.Color.prototype.getBlue=function()
 
92
{
 
93
  return this.blue;
 
94
}
 
95
 
 
96
/**
 
97
 * Returns the ideal Text Color. Usefull for font color selection by a given background color.
 
98
 *
 
99
 * @returns The <i>ideal</i> inverse color.
 
100
 * @type draw2d.Color
 
101
 **/
 
102
draw2d.Color.prototype.getIdealTextColor=function()
 
103
{
 
104
   var nThreshold = 105;
 
105
   var bgDelta = (this.red * 0.299) + (this.green * 0.587) + (this.blue * 0.114);
 
106
   return (255 - bgDelta < nThreshold) ? new  draw2d.Color(0,0,0) : new  draw2d.Color(255,255,255);
 
107
}
 
108
 
 
109
 
 
110
/**
 
111
 * @private
 
112
 */
 
113
draw2d.Color.prototype.hex2rgb=function(/*:String */hexcolor)
 
114
{
 
115
  hexcolor = hexcolor.replace("#","");
 
116
  return(
 
117
         {0:parseInt(hexcolor.substr(0,2),16),
 
118
          1:parseInt(hexcolor.substr(2,2),16),
 
119
          2:parseInt(hexcolor.substr(4,2),16)}
 
120
         );
 
121
}
 
122
 
 
123
/**
 
124
 * @private
 
125
 **/
 
126
draw2d.Color.prototype.hex=function()
 
127
 
128
  return(this.int2hex(this.red)+this.int2hex(this.green)+this.int2hex(this.blue)); 
 
129
}
 
130
 
 
131
/**
 
132
 * @private
 
133
 */
 
134
draw2d.Color.prototype.int2hex=function(v) 
 
135
{
 
136
  v=Math.round(Math.min(Math.max(0,v),255));
 
137
  return("0123456789ABCDEF".charAt((v-v%16)/16)+"0123456789ABCDEF".charAt(v%16));
 
138
}
 
139
 
 
140
/**
 
141
 * Returns a darker color of the given one..
 
142
 * 
 
143
 * @param {float} fraction  Darkness fraction.
 
144
 * @return        Darker color.
 
145
 * @type draw2d.Color
 
146
 */
 
147
draw2d.Color.prototype.darker=function(/*:float*/fraction)
 
148
{
 
149
   var red   = parseInt(Math.round (this.getRed()   * (1.0 - fraction)));
 
150
   var green = parseInt(Math.round (this.getGreen() * (1.0 - fraction)));
 
151
   var blue  = parseInt(Math.round (this.getBlue()  * (1.0 - fraction)));
 
152
 
 
153
   if (red   < 0) red   = 0; else if (red   > 255) red   = 255;
 
154
   if (green < 0) green = 0; else if (green > 255) green = 255;
 
155
   if (blue  < 0) blue  = 0; else if (blue  > 255) blue  = 255;
 
156
 
 
157
   return new draw2d.Color(red, green, blue);
 
158
}
 
159
 
 
160
 
 
161
/**
 
162
 * Make a color lighter.
 
163
 * 
 
164
 * @param {float} fraction  Darkness fraction.
 
165
 * @type draw2d.Color
 
166
 * @return          Lighter color.
 
167
 */
 
168
draw2d.Color.prototype.lighter=function(/*:float*/ fraction)
 
169
{
 
170
    var red   = parseInt(Math.round (this.getRed()   * (1.0 + fraction)));
 
171
    var green = parseInt(Math.round (this.getGreen() * (1.0 + fraction)));
 
172
    var blue  = parseInt(Math.round (this.getBlue()  * (1.0 + fraction)));
 
173
 
 
174
    if (red   < 0) red   = 0; else if (red   > 255) red   = 255;
 
175
    if (green < 0) green = 0; else if (green > 255) green = 255;
 
176
    if (blue  < 0) blue  = 0; else if (blue  > 255) blue  = 255;
 
177
 
 
178
    return new draw2d.Color(red, green, blue);
 
179
}