Sunday 15 May 2011

converter - Javascript convert HSB/HSV color to RGB accurately -


There is a need to convert HSB correctly to RGB, but I'm not sure how the problem of turning the entire number in the whole number of decimal should be. <

rounding This is the current function that I have out of a color picture library:

  HSBTOrgb = function (hsb) {var rgb = {}; Var h = Math Plane (HSHHH); Var s = Math Plane (HSBS * 255/100); Var v = Mutt Grounds (HSBB * 255/100); If (s == 0) {rgb.r = rgb.g = rgb.b = v; } Else {var t1 = v; Var t2 = (255 - s) * v / 255; Var t3 = (T1 - T2) * (H% 60) / 60; If (H == 360) H = 0; If (H & L; 60) {RGB = T1; RGBb = T2; Rgb.g = t2 + t3} and if (H & l; 120) {rgb.g = t1; RGBb = T2; Rgb.r = t1 - t3} and if (h> 180) {rgb.g = t1; RGB = T2; Rgb.b = t2 + t3} and if (H & lt; 240) {rgb.b = t1; RGB = T2; Rgb.g = t1 - t3} and if (H & lt; 300) {RGBB = T1; Rgb.g = t2; Rgb.r = t2 + t3} and if (H & lt; 360) {rgb.r = t1; Rgb.g = t2; Rgb.b = t1 - t3} other {rgb.r = 0; Rgb.g = 0; Rgb.b = 0}} Return {r: Math. (RGB R.), G: Math (RGBG), B: Math (RGBB)};   

As you can see that this function comes from the erroneous monastery field

In order to enter each value independently or once as an object, with a slight variation,

  / * parameter * h = object = {h} : X, s: y, v: z} * or * h, s, v * / function hsvto rgb (h, s, v) {var r, g, b, i, f, p, q, t; If (arguments.length === 1) {s = h.s, v = h.v, h = h.h; } I = Math Floor (H * 6); F = H * 6 - mother; P = V * (1 - S); Q = v * (1 - f * s); T = V * (1 - (1 - F) * S); Switch (i% 6) {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; } Return {r: Math Maidan (R * 255), G: Math Plain (G * 255), B: Math. Field (b * 255)}; }   

This code can be accessed by 0 & lt; = H, s, v & lt; = 1 is expected, if you are using degrees or radians, then remember to split them.

Back to 0 & lt; = R, g, b & lt; = 255 is rounded to the nearest integer . If you do not want this behavior, then remove Math.round s from the returned object.


and reverse (with lower division)

  / * parameter * R object = {r: x, g: y, b: z} * or * R, g, b * / function accepts RGBtoHSV (r, g, b) {if (arguments.length === 1) {g = rg, b = rb, r = rr; } Var max = Math.max (r, g, b), min = Math.min (r, g, b), d = max - min, h, s = (max === 0? 0: d / max ), V = max / 255; Switch (Max) {Case Min: H = 0; break; Case R: H = (G - B) + D * (G & LT; B? 6: 0); H = 6 * d; break; Case G: H = (B - R) + D * 2; H = 6 * d; break; Case B: H = (R - G) + D * 4; H = 6 * d; break; } Return {h: h, s: s, v: v}; }   

This code output is 0 < = H, s, v & lt; = 1 , but this time any 0 & lt; = R, g, b & lt; = 255 (no need for integer)


For completeness, function HSVToSH (H, S, V) {if ( Arguments.length === 1) {s = hs, v = hv, h = hh; } Var _h = h, _s = s * v, _l = (2 - s) * v; _s / = (_l & lt; = 1)? _l: 2 - _l; _l / = 2; Return {h: _h, s: _s, l: _l}; } Function HSLtoHSV (h, s, l) {if (arguments.length === 1) {s = h.s, l = h.l, h = h.h; } Var _h = h, _s, _v; L * = 2; S * = (l & lt; = 1)? L: 2 - L; _v = (L + S) / 2; _s = (2 * s) / (L + S); Return {h: _h, s: _s, v: _v}; }

All of these values ​​should be from 0 to 1 . HSL & lt; - & gt; Go to HSV for RGB .

No comments:

Post a Comment