home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************
- ADOBE SYSTEMS INCORPORATED
- Copyright 2002 Adobe Systems Incorporated
- All Rights Reserved
-
- NOTICE: Adobe permits you to use, modify, and distribute this
- file in accordance with the terms of the Adobe license agreement
- accompanying it. If you have received this file from a source
- other than Adobe, then your use, modification, or distribution
- of it requires the prior written permission of Adobe.
- ***************************************************************/
- /***************************************************************
- Author: Mary Obelnicki
- ***************************************************************/
-
- /***************************************************************
-
- This include provides two functions that facilitate converting
- to and from RGB to HSB colors.
-
-
- rgbTohsb(rgbObj)
- converts from rgb color to hsb color.
-
- returns a generic object with three properties, hue, saturation
- and brightness, that contain the corresponding color values.
- hue ranges from 0 to 360. Saturation and brigntness range
- from 0 to 100.
-
- Arguments:
- <rgbObj> an object with three properties, red, green and
- blue which contain the corresponding rgb color values.
- Valid values for red green and blue are 0 to 255, inclusive.
-
-
- hsbTorgb(hsbObj)
- converts from hsb color to rgb color.
-
- returns a generic object with three properties, red, green
- and blue that contain the corresponding color values. Valid
- values for red green and blue are 0 to 255, inclusive.
- NOTE: the object returned is not a LMColor and can not be assign to
- an LMColor.
-
- Arguments:
- <hsbObj> an object with three properties, hue, saturation and brightness
- that contain the corresponding color values. Value ranges for the properties
- are: hue from 0 to 360. Saturation and brigntness from 0 to 100.
-
- ***************************************************************/
-
- /***************************************************************
- DO NOT EDIT BELOW THIS LINE
- ***************************************************************/
-
-
- function rgbTohsb(rgbObj)
- {
- //convert rgb to 0 to 1 range;
- var r = rgbObj.red/255;
- var g = rgbObj.green/255;
- var b = rgbObj.blue/255;
-
-
- //some error handling
- //test written this strange way to catch undefineds as well as
- //invalid values
- if(!((r>=0) && (r<= 1) && (g>=0) && (g<= 1)&& (b>=0) && (b<= 1)))
- {// error
- Console.write("bad arguement to rgbTohsb()\n");
- return;
- }
-
- var max = Math.max(r, Math.max(g, b));
- var min = Math.min(r, Math.min(g, b));
-
- var h;
- var s;
- var v;
-
- v = max;
- s = (max != 0) ? ((max - min)/max) : 0.0;
-
- if(s == 0)
- h = 0.0;
- else
- {
- var delta = max - min;
- if(r == max)
- h = (g - b)/delta;
- else if(g == max)
- h = 2 + (b - r)/delta;
- else if(b == max)
- h = 4 + (r - g)/delta;
-
- h *= 60;
- if(h < 0)
- h += 360;
- }
-
- // convert hsv to LM representation of hsb
- var hsbObj = new Object();
- hsbObj.hue = Math.round(h);
- hsbObj.saturation = Math.round(s*100);
- hsbObj.brightness = Math.round(v*100);
-
- return hsbObj;
- }
- function hsbTorgb(hsbObj)
- {
- // created to avoid changing passed in hsb object
- var h = hsbObj.hue;
- var s = hsbObj.saturation/100;
- var v = hsbObj.brightness/100;
- if(!((h>=0) && (h<=360) && (s>=0) && (s<=1)&& (v>=0) && (v<=1)))
- {// error
- Console.write("bad arguement to hsbTorgb()\n");
- return;
- }
-
- var r, g, b;
-
- if(s == 0)
- {
- r = v;
- g = v;
- b = v;
- }
- else
- {
- var f, p, q, t, i;
- if (h == 360)
- h = 0;
- h /= 60;
- i = Math.floor(h);
- f = h - i;
- p = v * (1 - s);
- q = v * (1 - (s * f));
- t = v * (1 - (s * (1 - f)));
- switch(i)
- {
- case 0:
- r = v; g = t; b = p; break;
- case 1:
- r = q; g = v; b = p; break;
- case 2:
- r = p; g = v; b = t; break;
- case 3:
- r = p; g = q; b = v; break;
- case 4:
- r = t; g = p; b = v; break;
- case 5:
- r = v; g = p; b = q; break;
- }
- }
- rgbObj = new Object();
- rgbObj.red = Math.round(r * 255);
- rgbObj.green = Math.round(g * 255);
- rgbObj.blue = Math.round(b * 255);
- return rgbObj;
- }
-
-
-