Skip to content

Instantly share code, notes, and snippets.

@ZakBlystone
Created January 8, 2022 09:12
Show Gist options
  • Save ZakBlystone/ae5bd6a21def7ca3713fdfae8080019c to your computer and use it in GitHub Desktop.
Save ZakBlystone/ae5bd6a21def7ca3713fdfae8080019c to your computer and use it in GitHub Desktop.
GLua code for fast conversion between RGB / HSV without using Color objects
-- Color conversion (CC0 Public Domain, free to use in whatever you want)
-- Created by: Zachary Blystone ( [email protected] )
local _min, _max, _abs, _floor = math.min, math.max, math.abs, math.floor
local _permut = {
function(a,b,c) return a,b,c end,
function(a,b,c) return b,a,c end,
function(a,b,c) return c,a,b end,
function(a,b,c) return c,b,a end,
function(a,b,c) return b,c,a end,
function(a,b,c) return a,c,b end,
}
local function rgb(h,s,v)
h = h % 360 / 60
local x = v * s * _abs(h % 2 - 1)
return _permut[1+_floor(h)](v,v-x,v-v*s)
end
local function hsv(r,g,b)
local v = _max(r,g,b)
local c = v - _min(r,g,b)
if c * v == 0 then return 0, 0, v
elseif v == r then return 60 * (g-b) / c % 360, c/v, v
elseif v == g then return 120 + 60 * (b-r) / c, c/v, v
elseif v == b then return 240 + 60 * (r-g) / c, c/v, v
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment