1
/* This notice must be untouched at all times.
4
The latest version is available at
5
http://www.openjacob.org
7
Copyright (c) 2006 Andreas Herz. All rights reserved.
8
Created 5. 11. 2006 by Andreas Herz (Web: http://www.freegroup.de )
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.
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.
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
31
* @author Andreas Herz
34
draw2d.Color=function(/*:int*/ red,/*:int*/ green ,/*:int*/ blue)
36
if(typeof green == "undefined")
38
var rgb = this.hex2rgb(red);
57
draw2d.Color.prototype.type="Color";
62
draw2d.Color.prototype.getHTMLStyle=function()
64
return "rgb("+this.red+","+this.green+","+this.blue+")";
68
* Return the [red] part of the color.
71
draw2d.Color.prototype.getRed=function()
78
* Return the [green] part of the color.
81
draw2d.Color.prototype.getGreen=function()
88
* Return the [blue] part of the color.
91
draw2d.Color.prototype.getBlue=function()
97
* Returns the ideal Text Color. Usefull for font color selection by a given background color.
99
* @returns The <i>ideal</i> inverse color.
102
draw2d.Color.prototype.getIdealTextColor=function()
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);
113
draw2d.Color.prototype.hex2rgb=function(/*:String */hexcolor)
115
hexcolor = hexcolor.replace("#","");
117
{0:parseInt(hexcolor.substr(0,2),16),
118
1:parseInt(hexcolor.substr(2,2),16),
119
2:parseInt(hexcolor.substr(4,2),16)}
126
draw2d.Color.prototype.hex=function()
128
return(this.int2hex(this.red)+this.int2hex(this.green)+this.int2hex(this.blue));
134
draw2d.Color.prototype.int2hex=function(v)
136
v=Math.round(Math.min(Math.max(0,v),255));
137
return("0123456789ABCDEF".charAt((v-v%16)/16)+"0123456789ABCDEF".charAt(v%16));
141
* Returns a darker color of the given one..
143
* @param {float} fraction Darkness fraction.
144
* @return Darker color.
147
draw2d.Color.prototype.darker=function(/*:float*/fraction)
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)));
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;
157
return new draw2d.Color(red, green, blue);
162
* Make a color lighter.
164
* @param {float} fraction Darkness fraction.
166
* @return Lighter color.
168
draw2d.Color.prototype.lighter=function(/*:float*/ fraction)
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)));
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;
178
return new draw2d.Color(red, green, blue);