LuaBox
Loading...
Searching...
No Matches
HSV.h
Go to the documentation of this file.
1#pragma once
2
3#include "lua_include.h"
4#include "global.h"
5#include <cmath>
6
16
17int HSV(lua_State* L_)
18{
19 double h = luaL_checknumber(L_, 1);
20 double s = luaL_checknumber(L_, 2);
21 double v = luaL_checknumber(L_, 3);
22
23 if (s < 0) s = 0; else if (s > 1) s = 1;
24 if (v < 0) v = 0; else if (v > 1) v = 1;
25 while (h < 0) h += 360;
26 while (h >= 360) h -= 360;
27
28 double c = v * s;
29 double x = c * (1 - fabs(fmod(h / 60.0, 2) - 1));
30 double m = v - c;
31
32 double r1, g1, b1;
33 if (h < 60) { r1 = c; g1 = x; b1 = 0; }
34 else if (h < 120) { r1 = x; g1 = c; b1 = 0; }
35 else if (h < 180) { r1 = 0; g1 = c; b1 = x; }
36 else if (h < 240) { r1 = 0; g1 = x; b1 = c; }
37 else if (h < 300) { r1 = x; g1 = 0; b1 = c; }
38 else { r1 = c; g1 = 0; b1 = x; }
39
40 int r = (int)((r1 + m) * 31);
41 int g = (int)((g1 + m) * 63);
42 int b = (int)((b1 + m) * 31);
43
44 uint16_t color = (r << 11) | (g << 5) | b;
45 lua_pushinteger(L_, color);
46
47 return 1;
48}
int HSV(lua_State *L_)
Convert HSV color to 16-bit RGB565.
Definition HSV.h:17