1
1
Fork 0
Browse Source

Fix bug in hsv-rgb conversion

thread-physics
alistair 4 years ago
parent
commit
d184ac6d00
  1. 61
      colours.c

61
colours.c

@ -1,6 +1,5 @@ @@ -1,6 +1,5 @@
#include <SDL2/SDL.h>
#include <stdbool.h>
#include <time.h>
SDL_Color get_random_color(unsigned int seed) {
int red = rand_r(&seed) % 255;
@ -39,7 +38,6 @@ bool m_equal(double a, double b) { @@ -39,7 +38,6 @@ bool m_equal(double a, double b) {
return (a - b) * (a - b) < 0.00001;
}
enum colour_space {
CS_HSV,
CS_HSL,
@ -54,7 +52,9 @@ struct colour { @@ -54,7 +52,9 @@ struct colour {
enum colour_space sp;
};
// h = [0,360], s = [0,1], v = [0,1] // if s == 0, then h = -1 (undefined)
// https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB
// h = [0,360], s = [0,1], v = [0,1]
// if s == 0, then h = -1 (undefined)
struct colour get_hs_l_v(SDL_Color c, enum colour_space sp) {
struct colour ret;
memset(&ret, 0, sizeof(struct colour));
@ -116,12 +116,13 @@ struct colour get_hsv(SDL_Color c) { @@ -116,12 +116,13 @@ struct colour get_hsv(SDL_Color c) {
return get_hs_l_v(c, CS_HSV);
}
// https://en.wikipedia.org/wiki/HSI_color_space
double magic_hsv_function(int n, struct colour c) {
double res = c.v - c.v * c.s;
double res = 0;
double k = fmod(n + (c.h / 60), 6);
double arr[] = {k, 4 - k, 1};
res = res * m_max(arr, 3);
double k_b = m_min(arr, 3);
double k_c = 0 > k_b ? 0 : k_b;
res = c.v - c.v * c.s * k_c;
return res;
}
@ -216,35 +217,23 @@ void test(int seed) { @@ -216,35 +217,23 @@ void test(int seed) {
printf("\n");
}
void wheel() {
struct colour c;
c.s = 0.999;
c.v = 0.99;
c.l = 0.5;
c.sp = CS_HSV;
for(int i = 0; i < 360; i+=5) {
c.h = (double)i;
SDL_Color rgb = get_rgb(c);
/*printf("HSL: %f %f %f\n",c.h, c.s, c.l);*/
/*printf("RGB: %d %d %d\n",rgb.r,rgb.g,rgb.b);*/
print_colour(rgb);
}
}
int main() {
test(1231);
test(2131);
test(123);
test(121);
test(1802);
}
//"#d48d47
//"#88934c
//"#c88153
//"#7c9f58
//"#bc755f
//"#c67c64
//"#cc825e
//"#ba6f70
//"#d98e51
//"#ad637d
//"#221534
//"#241336
//"#1f1831
//"#271423
//"#1c1b2e
//"#77f0a6
//"#83fb9a
//"#61dabc
//"#0b128b
//"#4ac3d3
wheel();
}

Loading…
Cancel
Save