From: Steinar H. Gunderson Date: Mon, 1 Oct 2012 13:10:48 +0000 (+0200) Subject: Split off hsv2rgb() in a separate file. X-Git-Tag: 1.0~484 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=02d19b8323a1b626acd9d3b4f08bb63f51149cf3;hp=8df9b9f19175effb728f3b587a65dd753457670e;p=movit Split off hsv2rgb() in a separate file. --- diff --git a/Makefile b/Makefile index af7ca8e..b8b0f37 100644 --- a/Makefile +++ b/Makefile @@ -3,13 +3,13 @@ CXX=g++ CXXFLAGS=-Wall LDFLAGS=-lSDL -lSDL_image -lGL -test: test.o - $(CXX) -o test test.o $(LDFLAGS) +test: test.o util.o + $(CXX) -o test test.o util.o $(LDFLAGS) .o: .cpp $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $< clean: - $(RM) test test.o + $(RM) test test.o util.o .PHONY: clean diff --git a/test.cpp b/test.cpp index 634a0ea..6de5452 100644 --- a/test.cpp +++ b/test.cpp @@ -20,6 +20,8 @@ #include #include +#include "util.h" + #ifdef NDEBUG #define check_error() #else @@ -92,48 +94,6 @@ enum textures { HSV_WHEEL = 4, }; -// assumes h in [0, 2pi> or [-pi, pi> -void hsv2rgb(float h, float s, float v, float *r, float *g, float *b) -{ - if (h < 0.0f) { - h += 2.0f * M_PI; - } - float c = v * s; - float hp = (h * 180.0 / M_PI) / 60.0; - float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f)); - - if (hp >= 0 && hp < 1) { - *r = c; - *g = x; - *b = 0.0f; - } else if (hp >= 1 && hp < 2) { - *r = x; - *g = c; - *b = 0.0f; - } else if (hp >= 2 && hp < 3) { - *r = 0.0f; - *g = c; - *b = x; - } else if (hp >= 3 && hp < 4) { - *r = 0.0f; - *g = x; - *b = c; - } else if (hp >= 4 && hp < 5) { - *r = x; - *g = 0.0f; - *b = c; - } else { - *r = c; - *g = 0.0f; - *b = x; - } - - float m = v - c; - *r += m; - *g += m; - *b += m; -} - GLhandleARB read_shader(const char* filename, GLenum type) { static char buf[131072]; diff --git a/util.cpp b/util.cpp new file mode 100644 index 0000000..592e4d5 --- /dev/null +++ b/util.cpp @@ -0,0 +1,44 @@ +#include + +#include "util.h" + +void hsv2rgb(float h, float s, float v, float *r, float *g, float *b) +{ + if (h < 0.0f) { + h += 2.0f * M_PI; + } + float c = v * s; + float hp = (h * 180.0 / M_PI) / 60.0; + float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f)); + + if (hp >= 0 && hp < 1) { + *r = c; + *g = x; + *b = 0.0f; + } else if (hp >= 1 && hp < 2) { + *r = x; + *g = c; + *b = 0.0f; + } else if (hp >= 2 && hp < 3) { + *r = 0.0f; + *g = c; + *b = x; + } else if (hp >= 3 && hp < 4) { + *r = 0.0f; + *g = x; + *b = c; + } else if (hp >= 4 && hp < 5) { + *r = x; + *g = 0.0f; + *b = c; + } else { + *r = c; + *g = 0.0f; + *b = x; + } + + float m = v - c; + *r += m; + *g += m; + *b += m; +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..0d49945 --- /dev/null +++ b/util.h @@ -0,0 +1,7 @@ +#ifndef _UTIL_H +#define _UTIL_H 1 + +// assumes h in [0, 2pi> or [-pi, pi> +void hsv2rgb(float h, float s, float v, float *r, float *g, float *b); + +#endif // !defined(_UTIL_H)