summaryrefslogtreecommitdiff
path: root/color.c
diff options
context:
space:
mode:
Diffstat (limited to 'color.c')
-rw-r--r--color.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/color.c b/color.c
new file mode 100644
index 0000000..9522fd5
--- /dev/null
+++ b/color.c
@@ -0,0 +1,40 @@
+
+#include <math.h>
+
+#include "color.h"
+
+
+void hsv_to_rgb(struct hsvf *hsv, struct rgbf *rgb) {
+ float h = hsv->h, s = hsv->s, v = hsv->v;
+ float c = v * s; // Chroma
+ float hmod = fmodf(h * 6.0f, 6.0f);
+ float x = c * (1.0f - fabsf(fmodf(hmod, 2.0f) - 1.0f));
+ float m = v - c;
+
+ switch ((int)hmod) {
+ case 0:
+ rgb->r = c; rgb->g = x; rgb->b = 0;
+ break;
+ case 1:
+ rgb->r = x; rgb->g = c; rgb->b = 0;
+ break;
+ case 2:
+ rgb->r = 0; rgb->g = c; rgb->b = x;
+ break;
+ case 3:
+ rgb->r = 0; rgb->g = x; rgb->b = c;
+ break;
+ case 4:
+ rgb->r = x; rgb->g = 0; rgb->b = c;
+ break;
+ case 5:
+ rgb->r = c; rgb->g = 0; rgb->b = x;
+ break;
+ default:
+ rgb->r = 0; rgb->g = 0; rgb->b = 0;
+ }
+
+ rgb->r += m;
+ rgb->g += m;
+ rgb->b += m;
+}