
Share Your Code
Browse Code
- All (902)
-
(94)
-
(84)
-
(44)
-
(4)
-
(42)
-
(135)
-
(21)
-
(41)
-
(13)
-
(65)
-
(150)
-
(209)
HSV to RGB
A little snippet to help you converting HSV Colors to RGB.
Useful if you want to have a well saturated Color or to get variations of a color!
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | display.setStatusBar(display.HiddenStatusBar) local function hsvToRgb(h, s, v) local r, g, b local i local f, p, q, t local colorArray = {} h = math.max(0, math.min(360, h)) s = math.max(0, math.min(100, s)) v = math.max(0, math.min(100, v)) s = s/100 v = v/100 if(s == 0) then r,g,b = v,v,v colorArray[1] = math.round(r * 255) colorArray[2] = math.round(g * 255) colorArray[3] = math.round(b * 255) return colorArray end h = h/60 i = math.floor(h) f = h - i p = v * (1 - s) q = v * (1 - s * f) t = v * (1 - s * (1 - f)) if (i==0) then r = v; g = t; b = p; elseif (i==1) then r = q; g = v; b = p; elseif (i==2) then r = p; g = v; b = t; elseif (i==3) then r = p; g = q; b = v; elseif (i==4) then r = t; g = p; b = v; elseif (i==5) then r = v; g = p; b = q; end colorArray[1] = math.round(r * 255) colorArray[2] = math.round(g * 255) colorArray[3] = math.round(b * 255) return colorArray end -- Create background local background = display.newRect(0,0,display.contentWidth,display.contentHeight) changeColor = function(evt) -- get a random saturated color local color = hsvToRgb(math.random(0,360),100,100) background:setFillColor(color[1],color[2],color[3]) end changeColor(0) background:addEventListener("tap",changeColor) |
- Type:
- Tags:
- rgb,
- color,
- hsv,
- conversion,
- change