Add HSV color routines.

This commit is contained in:
Daniel O'Connor 2014-08-24 16:54:25 +09:30
parent a7b3a867c9
commit 3b7cb1c516
2 changed files with 87 additions and 9 deletions

View File

@ -308,28 +308,106 @@ void jswrap_graphics_setPixel(JsVar *parent, int x, int y, JsVar *color) {
gfx.data.cursorY = (short)y;
}
// Convert HSV to RGB
// From http://www.cs.rit.edu/~ncs/color/t_convert.html
static void HSVtoRGB(JsVarFloat h, JsVarFloat s, JsVarFloat v, JsVarFloat *r, JsVarFloat *g, JsVarFloat *b) {
int i;
JsVarFloat f, p, q, t;
if (s == 0)
// achromatic (grey)
*r = *g = *b = v;
h = h / 60; // sector 0 to 5
i = (int)h;
f = h - i; // fractional part of h
p = v * (1 - s);
q = v * (1 - s * f);
t = v * (1 - s * (1 - f));
switch(i) {
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;
default:
*r = v;
*g = p;
*b = q;
break;
}
};
/*JSON{ "type":"method", "class": "Graphics", "name" : "setColor",
"description" : "Set the color to use for subsequent drawing operations",
"generate_full" : "jswrap_graphics_setColorX(parent, r,g,b, true)",
"params" : [ [ "r", "JsVar", "Red (between 0 and 1) OR an integer representing the color in the current bit depth" ],
"generate_full" : "jswrap_graphics_setColorX(parent, r,g,b, true, false)",
"params" : [ [ "r", "JsVar", "Red (between 0 and 1) OR an integer representing the color in the current bit depth and color order" ],
[ "g", "JsVar", "Green (between 0 and 1)" ],
[ "b", "JsVar", "Blue (between 0 and 1)" ] ]
}*/
/*JSON{ "type":"method", "class": "Graphics", "name" : "setBgColor",
"description" : "Set the background color to use for subsequent drawing operations",
"generate_full" : "jswrap_graphics_setColorX(parent, r,g,b, false)",
"params" : [ [ "r", "JsVar", "Red (between 0 and 1) OR an integer representing the color in the current bit depth" ],
"generate_full" : "jswrap_graphics_setColorX(parent, r,g,b, false, false)",
"params" : [ [ "r", "JsVar", "Red (between 0 and 1) OR an integer representing the color in the current bit depth and color order" ],
[ "g", "JsVar", "Green (between 0 and 1)" ],
[ "b", "JsVar", "Blue (between 0 and 1)" ] ]
}*/
void jswrap_graphics_setColorX(JsVar *parent, JsVar *r, JsVar *g, JsVar *b, bool isForeground) {
/*JSON{ "type":"method", "class": "Graphics", "name" : "setColorHSV",
"description" : "Set the HSV color to use for subsequent drawing operations",
"generate_full" : "jswrap_graphics_setColorX(parent, h,s,v, true, true)",
"params" : [ [ "h", "JsVar", "Hue (between 0 and 1)" ],
[ "s", "JsVar", "Saturation (between 0 and 1)" ],
[ "v", "JsVar", "Value (between 0 and 1)" ] ]
}*/
/*JSON{ "type":"method", "class": "Graphics", "name" : "setBgColorHSV",
"description" : "Set the background HSV color to use for subsequent drawing operations",
"generate_full" : "jswrap_graphics_setColorX(parent, h,s,v, false, true)",
"params" : [ [ "h", "JsVar", "Hue (between 0 and 1)" ],
[ "s", "JsVar", "Saturation (between 0 and 1)" ],
[ "v", "JsVar", "Value (between 0 and 1)" ] ]
}*/
void jswrap_graphics_setColorX(JsVar *parent, JsVar *r, JsVar *g, JsVar *b, bool isForeground, bool isHSV) {
JsGraphics gfx; if (!graphicsGetFromVar(&gfx, parent)) return;
unsigned int color = 0;
JsVarFloat rf, gf, bf;
if (isHSV) {
if (jsvIsUndefined(r) || jsvIsUndefined(g) || jsvIsUndefined(b)) {
jsExceptionHere(JSET_ERROR, "h, s and v must be defined");
return;
}
JsVarFloat hf = jsvGetFloat(r);
JsVarFloat sf = jsvGetFloat(g);
JsVarFloat vf = jsvGetFloat(b);
HSVtoRGB(hf, sf, vf, &rf, &gf, &bf);
} else {
rf = jsvGetFloat(r);
gf = jsvGetFloat(g);
bf = jsvGetFloat(b);
}
if (!jsvIsUndefined(g) && !jsvIsUndefined(b)) {
int ri = (int)(jsvGetFloat(r)*256);
int gi = (int)(jsvGetFloat(g)*256);
int bi = (int)(jsvGetFloat(b)*256);
int ri = (int)(rf*256);
int gi = (int)(gf*256);
int bi = (int)(bf*256);
if (ri>255) ri=255;
if (gi>255) gi=255;
if (bi>255) bi=255;

View File

@ -34,7 +34,7 @@ void jswrap_graphics_fillRect(JsVar *parent, int x1, int y1, int x2, int y2);
void jswrap_graphics_drawRect(JsVar *parent, int x1, int y1, int x2, int y2);
int jswrap_graphics_getPixel(JsVar *parent, int x, int y);
void jswrap_graphics_setPixel(JsVar *parent, int x, int y, JsVar *color);
void jswrap_graphics_setColorX(JsVar *parent, JsVar *r, JsVar *g, JsVar *b, bool isForeground);
void jswrap_graphics_setColorX(JsVar *parent, JsVar *r, JsVar *g, JsVar *b, bool isForeground, bool isHSV);
JsVarInt jswrap_graphics_getColorX(JsVar *parent, bool isForeground);
void jswrap_graphics_setFontSizeX(JsVar *parent, int size, bool checkValid);
void jswrap_graphics_setFontCustom(JsVar *parent, JsVar *bitmap, int firstChar, JsVar *width, int height);