Wang Wei Gang, April 2005
In this palette assembly that uses the HSB color ring, the most important basic function is the mutual conversion of HSB and RGB color space. It seems that many people are looking for similar code. In fact, the Color class in the Java API contains such functions, while Java SDK provides source code, we can change the Java code directly to the JavaScript code used by Flash. Here is my modification result:
Public Static Function HSB2RGB (Hue: Number, Saturation: Number, Brightness: Number: Number {VAR R, G, B, H, F, P, Q, T; Var RGBVALS = New Array (3); hue / = 360 , Saturation / = 100; Brightness / = 100; if (Saturation == 0) {r = g = b = Math.Floor (Brightness * 255.0 0.5);} else {h = (hue - Math.Floor (HUE) * 6.0; f = h - math.floor (h); p = brightness * (1.0 - saturation); Q = Brightness * (1.0 - saturation * f); t = brightness * (1.0 - (Saturation * (1.0 - f))))); Switch (Math.Floor (h)) {case 0: r = math.floor (Brightness * 255.0 0.5); g = math.floor (t * 255.0 0.5); b = math.fllor P * 255.0 0.5; Break; Case 1: r = Math.Floor (Q * 255.0 0.5); g = Math.Floor (Brightness * 255.0 0.5); b = Math.Floor (p * 255.0 0.5) Break; Case 2: r = Math.Floor (p * 255.0 0.5); g = Math.Floor (Brightness * 2555.0 0.5); b = Math.Floor (T * 255.0 0.5); Break; Case 3: R = Math.floor (p * 255.0 0.5); g = Math.Floor (q * 255.0 0.5 ); B = math.floor (Brightness * 255.0 0.5); Break; case 4: r = math.floor (t * 255.0 0.5); g = math.floor (p * 255.0 0.5); b = math. FLOOR (Brightness * 255.0 0.5); Break; Case 5: r = Math.Floor (Brightness * 255.0 0.5); g = Math.Floor (p * 255.0 0.5); b = math.floor (q * 255.0 0.5); Break;}} rgbvals [0] = r; Rgbvals [1] = g; RGBVALS [2] = B; RTURN RGBVALS;} Public Static Function HSB2RGBValue (Hue: Number, Sature: Number, Brightness: Number): Number {var rgbvals =
HSB2RGB (Hue, Saturation, Brightness); RGBVALS [0] << 16) | (RGBVALS [1] << 8) | (RGBVALS [2] << 0);} public static function RGB2HSB (r: Number, G: Number, B: Number: array {var hue, saturation, brightness; var hsbvals = new array (3); var cmax = (r> g)? R: g; if (b> cmax) cmax = B; VAR cmin = (r