diff options
-rw-r--r-- | include/painter/color.h | 61 | ||||
-rw-r--r-- | src/blender/color.c | 8 | ||||
-rw-r--r-- | src/painter/color.c | 20 | ||||
-rw-r--r-- | test/painter/color.c | 14 |
4 files changed, 95 insertions, 8 deletions
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 @@ | |||
1 | #ifndef UPEM_MORPHING_COLOR | ||
2 | #define UPEM_MORPHING_COLOR | ||
3 | |||
4 | /** | ||
5 | * File: color.h | ||
6 | * | ||
7 | * See also: | ||
8 | * A rainbow | ||
9 | */ | ||
10 | |||
11 | #include <MLV/MLV_color.h> | ||
12 | #include <stdbool.h> | ||
13 | #include "common/time.h" | ||
14 | |||
15 | /** | ||
16 | * Type: ColorComponent | ||
17 | * Represents a single colour component of 32-bits RGBa tuple. | ||
18 | */ | ||
19 | typedef uint8_t ColorComponent; | ||
20 | |||
21 | /** | ||
22 | * Type: ColorPixel | ||
23 | * Represents a single RGBa coloured pixel. | ||
24 | * Compatible with the libMLV representation. | ||
25 | */ | ||
26 | typedef union { | ||
27 | struct { | ||
28 | ColorComponent a, b, g, r; | ||
29 | } rgba; | ||
30 | |||
31 | MLV_Color mlv; | ||
32 | } Color; | ||
33 | |||
34 | /** | ||
35 | * Function: color_equals | ||
36 | * Compares the supplied colors. | ||
37 | * | ||
38 | * Parameters: | ||
39 | * c1 - the first color | ||
40 | * c2 - the second color | ||
41 | * | ||
42 | * Returns: | ||
43 | * T(c1 is the same color as c2) | ||
44 | */ | ||
45 | bool color_equals(Color c1, Color c2); | ||
46 | |||
47 | /** | ||
48 | * Function: color_blend | ||
49 | * Blends two colors. | ||
50 | * | ||
51 | * Parameters: | ||
52 | * origin - the first color | ||
53 | * target - the second color | ||
54 | * distance - the distance from the first color | ||
55 | * | ||
56 | * Returns: | ||
57 | * The blended color | ||
58 | */ | ||
59 | Color color_blend(Color origin, Color target, TimeVector distance); | ||
60 | |||
61 | #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 @@ | |||
1 | #include "blender/color.h" | ||
2 | |||
3 | bool color_equals(Color c1, Color c2) { | ||
4 | return c1.rgba.r == c2.rgba.r && | ||
5 | c1.rgba.g == c2.rgba.g && | ||
6 | c1.rgba.b == c2.rgba.b && | ||
7 | c1.rgba.a == c2.rgba.a; | ||
8 | } | ||
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 @@ | |||
1 | #include "painter/color.h" | ||
2 | #include <math.h> | ||
3 | |||
4 | static inline ColorComponent blend_component(ColorComponent origin, ColorComponent target, TimeVector frame) { | ||
5 | return (ColorComponent) round(sqrt((TIME_UNIT - frame) * pow(origin, 2) + frame * pow(target, 2))); | ||
6 | } | ||
7 | |||
8 | bool color_equals(Color c1, Color c2) { | ||
9 | return c1.rgba.r == c2.rgba.r && | ||
10 | c1.rgba.g == c2.rgba.g && | ||
11 | c1.rgba.b == c2.rgba.b && | ||
12 | c1.rgba.a == c2.rgba.a; | ||
13 | } | ||
14 | |||
15 | Color color_blend(Color origin, Color target, TimeVector distance) { | ||
16 | return (Color) {{blend_component(origin.rgba.a, target.rgba.a, distance), | ||
17 | blend_component(origin.rgba.b, target.rgba.b, distance), | ||
18 | blend_component(origin.rgba.g, target.rgba.g, distance), | ||
19 | blend_component(origin.rgba.r, target.rgba.r, distance)}}; | ||
20 | } | ||
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 @@ | |||
1 | #include "painter/color.h" | ||
2 | #include <assert.h> | ||
3 | |||
4 | static void test_color_blend() { | ||
5 | Color a = {{1, 10, 100, 200}}, b = {{100, 1, 200, 10}}; | ||
6 | assert(color_equals(color_blend(a, b, TIME_ORIGIN), a)); | ||
7 | assert(color_equals(color_blend(a, b, TIME_UNIT), b)); | ||
8 | assert(color_equals(color_blend(a, b, 0.25), (Color) {{50, 9, 132, 173}})); | ||
9 | } | ||
10 | |||
11 | int main(int argc, char **argv) { | ||
12 | test_color_blend(); | ||
13 | return 0; | ||
14 | } | ||