From 190449ee18bec69b2e385dccd9bd42ddc83dd418 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 28 Dec 2017 01:22:41 +0100 Subject: Refactor and test color Signed-off-by: pacien --- include/painter/color.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ src/blender/color.c | 8 ------- src/painter/color.c | 20 ++++++++++++++++ test/painter/color.c | 14 ++++++++++++ 4 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 include/painter/color.h delete mode 100644 src/blender/color.c create mode 100644 src/painter/color.c create mode 100644 test/painter/color.c diff --git a/include/painter/color.h b/include/painter/color.h new file mode 100644 index 0000000..2aeee3e --- /dev/null +++ b/include/painter/color.h @@ -0,0 +1,61 @@ +#ifndef UPEM_MORPHING_COLOR +#define UPEM_MORPHING_COLOR + +/** + * File: color.h + * + * See also: + * A rainbow + */ + +#include +#include +#include "common/time.h" + +/** + * Type: ColorComponent + * Represents a single colour component of 32-bits RGBa tuple. + */ +typedef uint8_t ColorComponent; + +/** + * Type: ColorPixel + * Represents a single RGBa coloured pixel. + * Compatible with the libMLV representation. + */ +typedef union { + struct { + ColorComponent a, b, g, r; + } rgba; + + MLV_Color mlv; +} Color; + +/** + * Function: color_equals + * Compares the supplied colors. + * + * Parameters: + * c1 - the first color + * c2 - the second color + * + * Returns: + * T(c1 is the same color as c2) + */ +bool color_equals(Color c1, Color c2); + +/** + * Function: color_blend + * Blends two colors. + * + * Parameters: + * origin - the first color + * target - the second color + * distance - the distance from the first color + * + * Returns: + * The blended color + */ +Color color_blend(Color origin, Color target, TimeVector distance); + +#endif diff --git a/src/blender/color.c b/src/blender/color.c deleted file mode 100644 index f92fba9..0000000 --- a/src/blender/color.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "blender/color.h" - -bool color_equals(Color c1, Color c2) { - return c1.rgba.r == c2.rgba.r && - c1.rgba.g == c2.rgba.g && - c1.rgba.b == c2.rgba.b && - c1.rgba.a == c2.rgba.a; -} diff --git a/src/painter/color.c b/src/painter/color.c new file mode 100644 index 0000000..65c4f20 --- /dev/null +++ b/src/painter/color.c @@ -0,0 +1,20 @@ +#include "painter/color.h" +#include + +static inline ColorComponent blend_component(ColorComponent origin, ColorComponent target, TimeVector frame) { + return (ColorComponent) round(sqrt((TIME_UNIT - frame) * pow(origin, 2) + frame * pow(target, 2))); +} + +bool color_equals(Color c1, Color c2) { + return c1.rgba.r == c2.rgba.r && + c1.rgba.g == c2.rgba.g && + c1.rgba.b == c2.rgba.b && + c1.rgba.a == c2.rgba.a; +} + +Color color_blend(Color origin, Color target, TimeVector distance) { + return (Color) {{blend_component(origin.rgba.a, target.rgba.a, distance), + blend_component(origin.rgba.b, target.rgba.b, distance), + blend_component(origin.rgba.g, target.rgba.g, distance), + blend_component(origin.rgba.r, target.rgba.r, distance)}}; +} diff --git a/test/painter/color.c b/test/painter/color.c new file mode 100644 index 0000000..bdfe9b3 --- /dev/null +++ b/test/painter/color.c @@ -0,0 +1,14 @@ +#include "painter/color.h" +#include + +static void test_color_blend() { + Color a = {{1, 10, 100, 200}}, b = {{100, 1, 200, 10}}; + assert(color_equals(color_blend(a, b, TIME_ORIGIN), a)); + assert(color_equals(color_blend(a, b, TIME_UNIT), b)); + assert(color_equals(color_blend(a, b, 0.25), (Color) {{50, 9, 132, 173}})); +} + +int main(int argc, char **argv) { + test_color_blend(); + return 0; +} -- cgit v1.2.3