(Flash) RGB and HSV - Math and (Quasi) physics in AS3

| No Comments | No TrackBacks

The RGB color model is commonly used in computers as a direct representation of additive color mixing which most display devices utilize, while HSV is a model to describe perceptual color relationships in more intuitive manner. RGB stands for red, green and blue, and HSV stands for hue, saturation and value

RGB values can be converted to HSV values and vice versa using the formulas below:

RGB to HSV

r, g, b ∈ [0,1]
max = max(r, g, b), min = min(r, g, b)

h =
0 | if max = min
(60 * (g - b) / (max - min) + 360) % 360 | if max = r
60 * (b - r) / (max - min) + 120 | if max = g
60 * (r - g) / (max - min) + 240 | if max = b

s =
0 | if max = 0
(max - min) / max | otherwise

v = max

HSV to RGB

h ∈ [0,360], s, v ∈ [0,1]

hi = floor(_h / 60) % 6
f = _h / 60 - floor(_h / 60)
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
			
(r,g, b) =
(v, t, p) | if hi = 0
(q, v, p) | if hi = 1
(p, v, t) | if hi = 2
(p, q, v) | if hi = 3
(t, p, v) | if hi = 4
(v, p, q) | if hi = 5

See 'RGB color model - Wikipedia' and 'HSL and HSV - Wikipedia' for more about RGB and HSV models.

sample movie and scripts

No TrackBacks

TrackBack URL: http://www.kynd.info/cp-bin/mt/mt-tb.cgi/106

Leave a comment