home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Include / colorConversion.js < prev    next >
Encoding:
JavaScript  |  2002-05-13  |  4.3 KB  |  164 lines

  1. /***************************************************************
  2. ADOBE SYSTEMS INCORPORATED 
  3. Copyright 2002 Adobe Systems Incorporated 
  4. All Rights Reserved 
  5.  
  6. NOTICE:  Adobe permits you to use, modify, and distribute this 
  7. file in accordance with the terms of the Adobe license agreement 
  8. accompanying it.  If you have received this file from a source 
  9. other than Adobe, then your use, modification, or distribution
  10. of it requires the prior written permission of Adobe. 
  11. ***************************************************************/
  12. /***************************************************************
  13. Author: Mary Obelnicki
  14. ***************************************************************/
  15.  
  16. /***************************************************************
  17.  
  18. This include provides two functions that facilitate converting 
  19. to and from RGB to HSB colors.
  20.  
  21.  
  22. rgbTohsb(rgbObj)
  23.     converts from rgb color to hsb color.
  24.  
  25.     returns a generic object with three properties, hue, saturation 
  26.     and brightness, that contain the corresponding color values. 
  27.     hue ranges from 0 to 360. Saturation and brigntness range 
  28.     from 0 to 100. 
  29.     
  30.     Arguments:
  31.         <rgbObj> an object with three properties, red, green and 
  32.         blue which contain the corresponding rgb color values. 
  33.         Valid values for red green and blue are 0 to 255, inclusive. 
  34.  
  35.  
  36. hsbTorgb(hsbObj)
  37.     converts from hsb color to rgb color. 
  38.  
  39.     returns a generic object with three properties, red, green 
  40.     and blue that contain the corresponding color values. Valid 
  41.     values for red green and blue are 0 to 255, inclusive. 
  42.     NOTE: the object returned is not a LMColor and can not be assign to 
  43.     an LMColor. 
  44.  
  45.     Arguments:
  46.         <hsbObj> an object with three properties, hue, saturation and brightness
  47.         that contain the corresponding color values. Value ranges for the properties 
  48.         are: hue from 0 to 360. Saturation and brigntness from 0 to 100. 
  49.  
  50. ***************************************************************/
  51.  
  52. /***************************************************************
  53. DO NOT EDIT BELOW THIS LINE
  54. ***************************************************************/
  55.  
  56.  
  57. function rgbTohsb(rgbObj)
  58. {
  59.     //convert rgb to 0 to 1 range; 
  60.     var r = rgbObj.red/255; 
  61.     var g = rgbObj.green/255; 
  62.     var b = rgbObj.blue/255; 
  63.  
  64.  
  65.     //some error handling
  66.     //test written this strange way to catch undefineds as well as 
  67.     //invalid values
  68.     if(!((r>=0) && (r<= 1) &&  (g>=0) && (g<= 1)&&  (b>=0) && (b<= 1)))
  69.     {// error
  70.     Console.write("bad arguement to rgbTohsb()\n"); 
  71.     return; 
  72.     }
  73.  
  74.     var max = Math.max(r, Math.max(g, b));
  75.     var min = Math.min(r, Math.min(g, b));
  76.  
  77.     var h; 
  78.     var s; 
  79.     var v; 
  80.  
  81.     v = max;
  82.     s = (max != 0) ? ((max - min)/max) : 0.0; 
  83.  
  84.     if(s == 0)
  85.     h = 0.0; 
  86.     else
  87.     {
  88.     var delta = max - min; 
  89.     if(r == max)
  90.         h = (g - b)/delta; 
  91.     else if(g == max)
  92.         h = 2 + (b - r)/delta; 
  93.     else if(b == max)
  94.         h = 4 + (r - g)/delta; 
  95.     
  96.     h *= 60; 
  97.     if(h < 0)
  98.         h += 360; 
  99.     }
  100.  
  101.     // convert hsv to LM representation of hsb
  102.     var hsbObj = new Object(); 
  103.     hsbObj.hue = Math.round(h); 
  104.     hsbObj.saturation = Math.round(s*100); 
  105.     hsbObj.brightness = Math.round(v*100); 
  106.  
  107.     return hsbObj; 
  108. }
  109. function hsbTorgb(hsbObj)
  110. {
  111.     // created to avoid changing passed in hsb object
  112.     var h = hsbObj.hue; 
  113.     var s = hsbObj.saturation/100; 
  114.     var v = hsbObj.brightness/100; 
  115.     if(!((h>=0) && (h<=360) &&  (s>=0) && (s<=1)&&  (v>=0) && (v<=1)))
  116.     {// error
  117.     Console.write("bad arguement to hsbTorgb()\n"); 
  118.     return; 
  119.     }
  120.  
  121.     var r, g, b; 
  122.  
  123.     if(s == 0)
  124.     {
  125.     r = v; 
  126.     g = v; 
  127.     b = v; 
  128.     }
  129.     else
  130.     {
  131.     var f, p, q, t, i; 
  132.     if (h == 360)
  133.         h = 0;
  134.     h /= 60; 
  135.     i = Math.floor(h); 
  136.     f = h - i; 
  137.     p = v * (1 - s); 
  138.     q = v * (1 - (s * f));
  139.     t = v * (1 - (s * (1 - f))); 
  140.     switch(i)
  141.     {
  142.         case 0: 
  143.         r = v; g = t; b = p; break; 
  144.         case 1: 
  145.         r = q; g = v; b = p; break; 
  146.         case 2: 
  147.         r = p; g = v; b = t; break; 
  148.         case 3: 
  149.         r = p; g = q; b = v; break; 
  150.         case 4: 
  151.         r = t; g = p; b = v; break; 
  152.         case 5: 
  153.         r = v; g = p; b = q; break; 
  154.     }
  155.     }
  156.     rgbObj = new Object(); 
  157.     rgbObj.red = Math.round(r * 255); 
  158.     rgbObj.green = Math.round(g * 255); 
  159.     rgbObj.blue = Math.round(b * 255); 
  160.     return rgbObj; 
  161. }
  162.  
  163.  
  164.