Skip to content

Commit

Permalink
Add HsvToRgb routine (backported from eed9a67)
Browse files Browse the repository at this point in the history
  • Loading branch information
cahirwpz committed Dec 16, 2023
1 parent a464886 commit 6654e49
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ extern u_char colortab[4096];
/* Each argument must be in range 0-15. */
u_short ColorTransition(u_short from, u_short to, u_short step);

u_short HsvToRgb(short h, short s, short v);

#endif
58 changes: 58 additions & 0 deletions lib/libgfx/HsvToRgb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <color.h>

/* Based on https://stackoverflow.com/a/14733008 */
u_short HsvToRgb(short h, short s, short v) {
short region, remainder, p, q, t;
u_char r, g, b;

if (s == 0) {
v &= 0xf0;
return (v << 4) | v | (v >> 4);
}

region = h / 43;
remainder = (h - (region * 43)) * 6;

p = (v * (short)(255 - s)) >> 8;
q = (v * (short)(255 - ((s * remainder) >> 8))) >> 8;
t = (v * (short)(255 - ((s * (short)(255 - remainder)) >> 8))) >> 8;

switch (region) {
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;
}

r &= 0xf0;
g &= 0xf0;
b &= 0xf0;

return (r << 4) | g | (b >> 4);
}
1 change: 1 addition & 0 deletions lib/libgfx/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ SOURCES := \
DeleteCopList.c \
DeletePixmap.c \
EndSprite.c \
HsvToRgb.c \
InitSharedBitmap.c \
InsideArea.c \
LoadPalette.c \
Expand Down

0 comments on commit 6654e49

Please sign in to comment.