AS3 HSB Colour (Object) to RGB Colour (Object)


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var darkGreenHsbObj:Object = new Object();
  2. darkGreenHsbObj.h = 90;
  3. darkGreenHsbObj.s = 100;
  4. darkGreenHsbObj.b = 40;
  5. var darkGreenRgbObj:Object = HSBtoRGB(darkGreenHsbObj.h, darkGreenHsbObj.s, darkGreenHsbObj.b);
  6. trace("HSB: " + darkGreenHsbObj.h + ", " + darkGreenHsbObj.s + ", " + darkGreenHsbObj.b);
  7. trace("RGB: " + darkGreenRgbObj.r + ", " + darkGreenRgbObj.g + ", " + darkGreenRgbObj.b);
  8.  
  9. function HSBtoRGB(h:int, s:int, b:int):Object
  10. {
  11. var rgb:Object = new Object();
  12. var max:Number = (b * 0.01) * 255;
  13. var min:Number = max * (1 - (s * 0.01));
  14.  
  15. if (h == 360) {
  16. h = 0;
  17. }
  18.  
  19. if (s == 0) {
  20. rgb.r = rgb.g = rgb.b = b*(255*0.01) ;
  21. } else {
  22. var _h:Number = Math.floor(h / 60);
  23.  
  24. switch(_h){
  25. case 0:
  26. rgb.r = max ;
  27. rgb.g = min+h * (max-min)/ 60;
  28. rgb.b = min;
  29. break;
  30. case 1:
  31. rgb.r = max-(h-60) * (max-min)/60;
  32. rgb.g = max;
  33. rgb.b = min;
  34. break;
  35. case 2:
  36. rgb.r = min ;
  37. rgb.g = max;
  38. rgb.b = min+(h-120) * (max-min)/60;
  39. break;
  40. case 3:
  41. rgb.r = min;
  42. rgb.g = max-(h-180) * (max-min)/60;
  43. rgb.b =max;
  44. break;
  45. case 4:
  46. rgb.r = min+(h-240) * (max-min)/60;
  47. rgb.g = min;
  48. rgb.b = max;
  49. break;
  50. case 5:
  51. rgb.r = max;
  52. rgb.g = min;
  53. rgb.b = max-(h-300) * (max-min)/60;
  54. break;
  55. case 6:
  56. rgb.r = max;
  57. rgb.g = min+h * (max-min)/ 60;
  58. rgb.b = min;
  59. break;
  60. }
  61.  
  62. rgb.r = Math.min(255, Math.max(0, Math.round(rgb.r)))
  63. rgb.g = Math.min(255, Math.max(0, Math.round(rgb.g)))
  64. rgb.b = Math.min(255, Math.max(0, Math.round(rgb.b)))
  65. }
  66. return rgb;
  67. }
  68.  
  69. // OUTPUT
  70. // HSB: 90, 100, 40
  71. // RGB: 51, 102, 0

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.