Image programming in actionscript3 games (18 consecutive times)

Source: Internet
Author: User
1.3.4 Implementation of the mutual conversion formula between HSB and RGB using ActionScript

To enable HSB to be reused in subsequent projects, we first turn this conversion formula into a tool class and put it into a public class library. Of course, such classes can also be directly used on the Internet (I personally recommend the frocessing Toolkit ). The algorithms in the class seem to be a little different from the formulas. On the one hand, I also learned from the Internet, and on the other hand, I set the values of S and L/B from 0 ~ 1 is changed to 0 ~ 100.

 

Package COM. gemei. geom {public class convertcolor {/*** Convert RGB to HSB * @ Param R int red channel value * @ Param g int green channel value * @ Param B INT blue channel value *@ return contains H, S and B attributes of the object (color phase 0 ~ 360, saturation 0 ~ 100, brightness 0 ~ 100) **/public static function rgbtohsb (r: int, G: int, B: INT): object {var HSB: Object = new object (); // The maximum and minimum values in RGB need to be used frequently in HSB mode. Therefore, store the variable var max: Number = math. max (R, G, B); var min: Number = math. min (R, G, B); // The calculation of saturation and brightness is relatively simple. Follow the formula to calculate HSB. S = (max! = 0 )? (Max-min)/MAX * 100: 0; HSB. B = max/255*100; // H is troublesome, but it is not complicated to write if (HSB. S = 0) {HSB. H = 0;} else {Switch (max) {Case R: HSB. H = (G-B)/(max-min) * 60 + 360) % 360; break; case G: HSB. H = (B-r)/(max-min) * 60 + 120; break; Case B: HSB. H = (r-g)/(max-min) * 60 + 240; break;} // This code prevents numeric overflow. In fact, as long as you are careful when calculating, the operation that controls the upper and lower limits can have no HSB. H = math. min (360, math. max (0, math. round (HSB. h) HSB. S = ma Th. min (100, math. max (0, math. round (HSB. s) HSB. B = math. min (100, math. max (0, math. round (HSB. b) return HSB;}/*** convert HSB to RGB (color phase 0 ~ 360, saturation 0 ~ 100, brightness 0 ~ 100) * @ Param H int color phase value * @ Param s int saturation value * @ Param B INT Brightness Value * @ return a value containing R, G and B attributes of the object **/public static function hsbtorgb (H: int, S: int, B: INT): object {var RGB: object = new object (); // according to the HSB formula converted from RGB, get the largest and smallest var MAX: Number = (B * 0.01) * 255; var min: number = max * (1-(S * 0.01); // then, based on the color phase calculation method, determine the value of Max and Min, if (H = 360) {H = 0;} If (S = 0) {RGB. R = RGB. G = RGB. B = B * (255*0.01);} else {VaR _ H: Number = math. FL OOR (H/60); Switch (_ H) {Case 0: RGB. R = max; RGB. G = min + H * (max-min)/60; RGB. B = min; break; Case 1: RGB. R = max-(h-60) * (max-min)/60; RGB. G = max; RGB. B = min; break; Case 2: RGB. R = min; RGB. G = max; RGB. B = min + (h-120) * (max-min)/60; break; Case 3: RGB. R = min; RGB. G = max-(h-180) * (max-min)/60; RGB. B = max; break; Case 4: RGB. R = min + (h-240) * (max-min)/60; RGB. G = min; RGB. B = max; brea K; Case 5: RGB. R = max; RGB. G = min; RGB. B = max-(h-300) * (max-min)/60; break; Case 6: RGB. R = max; RGB. G = min + H * (max-min)/60; RGB. B = min; break;} // a code RGB that prevents data overflow. R = math. min (255, math. max (0, math. round (RGB. r); RGB. G = math. min (255, math. max (0, math. round (RGB. g); RGB. B = math. min (255, math. max (0, math. round (RGB. b);} return RGB;} public static function rgbtohsl (r: int, G: int, B: int ): Object {var innerr: Number = r/255; var innerg: Number = g/255; var innerb: Number = B/255; var HSL: object = new object (); var min: Number = math. min (innerr, innerg, innerb); var MAX: Number = math. max (innerr, innerg, innerb); var delta: Number = max-min; HSL. L = (MAX + min)/2; If (delta = 0) {HSL. H = 0; HSL. S = 0} else {If (HSL. L <0.5) {HSL. S = delta/(MAX + min);} else {HSL. S = delta/(2-max-min) ;} If (HSL. S = 0) {HSL. H = 0;} else {Switch (max) {Case innerr: HSL. H = (innerg-innerb)/(max-min) * 60 + 360) % 360; break; Case innerg: HSL. H = (innerb-innerr)/(max-min) * 60 + 120; break; Case innerb: HSL. H = (innerr-innerg)/(max-min) * 60 + 240; break ;}} HSL. L * = 100; HSL. S * = 100; return HSL;}/*** convert HSL to RGB (H = 0 ~ 360, S = 0 ~ 100, L = 0 ~ 100) ***/public static function hsltorgb (H: int, S: int, L: INT): object {var RGB: Object = new object (); var innerh: number = H/360; var inners: Number = s/100; var innerl: Number = l/100; If (S = 0) {RGB. R = innerl * 255; RGB. G = innerl * 255; RGB. B = innerl * 255;} else {var var2: Number; If (innerl <0.5) {var2 = innerl * (1 + inners );} else {var2 = (innerl + inners)-(inners * innerl);} var var1: Number = 2 * innerl-var2; RGB. R = 255 * huetorgb (var1, var2, innerh + (1/3); RGB. G = 255 * huetorgb (var1, var2, innerh); RGB. B = 255 * huetorgb (var1, var2, innerh-(1/3);} return RGB;} Private Static function huetorgb (V1: Number, V2: Number, VH: number ): number {if) return (V1 + (v2-V1) * 6 * VL); If (2 * VL) <1) Return (V2); If (3 * VL) <2) return (V1 + (v2-V1) * (2/3)-otherwise) * 6); Return (V1 )}}}


 

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.