BRL-CAD
bu_color.c
Go to the documentation of this file.
1 /* B U _ C O L O R . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1985-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "bu.h"
28 
29 #include "vmath.h"
30 
31 static int
32 test_bu_rgb_to_hsv(int argc, char *argv[])
33 {
34  fastf_t expected_hsv_color[3];
35  fastf_t actual_hsv_color[3];
36  unsigned int scanned_rgb_color[3];
37  unsigned char rgb_color[3];
38 
39  if (argc != 4) {
40  bu_exit(1, "ERROR: input format is rgb_values expected_hsv_values [%s]\n", argv[0]);
41  }
42 
43  sscanf(argv[2], "%u,%u,%u", &scanned_rgb_color[RED], &scanned_rgb_color[GRN], &scanned_rgb_color[BLU]);
44  VMOVE(rgb_color, scanned_rgb_color);
45  sscanf(argv[3], "%lf,%lf,%lf", &expected_hsv_color[HUE], &expected_hsv_color[SAT], &expected_hsv_color[VAL]);
46 
47  bu_rgb_to_hsv(rgb_color, actual_hsv_color);
48 
49  printf("Result: %f,%f,%f", actual_hsv_color[HUE], actual_hsv_color[SAT], actual_hsv_color[VAL]);
50 
51  /* Use 0.01 as tolerance to allow the numbers in CMakeLists.txt to
52  * be a reasonable length.
53  */
54  return !(NEAR_EQUAL(expected_hsv_color[HUE], actual_hsv_color[HUE], 0.01)
55  && NEAR_EQUAL(expected_hsv_color[SAT], actual_hsv_color[SAT], 0.01)
56  && NEAR_EQUAL(expected_hsv_color[VAL], actual_hsv_color[VAL], 0.01));
57 }
58 
59 static int
60 test_bu_hsv_to_rgb(int argc, char *argv[])
61 {
62  unsigned int expected_rgb_color[3];
63  unsigned char actual_rgb_color[3];
64  fastf_t hsv_color[3];
65 
66  if (argc != 4) {
67  bu_exit(1, "ERROR: input format is hsv_values expected_rgb_values [%s]\n", argv[0]);
68  }
69 
70  sscanf(argv[2], "%lf,%lf,%lf", &hsv_color[HUE], &hsv_color[SAT], &hsv_color[VAL]);
71  sscanf(argv[3], "%u,%u,%u", &expected_rgb_color[RED], &expected_rgb_color[GRN], &expected_rgb_color[BLU]);
72 
73  bu_hsv_to_rgb(hsv_color, actual_rgb_color);
74 
75  printf("Result: %u,%u,%u", actual_rgb_color[RED], actual_rgb_color[GRN], actual_rgb_color[BLU]);
76 
77  return !(expected_rgb_color[RED] == actual_rgb_color[RED]
78  && expected_rgb_color[GRN] == actual_rgb_color[GRN]
79  && expected_rgb_color[BLU] == actual_rgb_color[BLU]);
80 }
81 
82 static int
83 test_bu_str_to_rgb(int argc, char *argv[])
84 {
85  unsigned int expected_rgb_color[3];
86  unsigned char actual_rgb_color[3];
87  char *rgb_string;
88 
89  if (argc != 4) {
90  bu_exit(1, "ERROR: input format is rgb_string expected_rgb_values [%s]\n", argv[0]);
91  }
92 
93  rgb_string = argv[2];
94  sscanf(argv[3], "%u,%u,%u", &expected_rgb_color[RED], &expected_rgb_color[GRN], &expected_rgb_color[BLU]);
95 
96  bu_str_to_rgb(rgb_string, actual_rgb_color);
97 
98  printf("Result: %u,%u,%u", actual_rgb_color[RED], actual_rgb_color[GRN], actual_rgb_color[BLU]);
99 
100  return !(expected_rgb_color[RED] == actual_rgb_color[RED]
101  && expected_rgb_color[GRN] == actual_rgb_color[GRN]
102  && expected_rgb_color[BLU] == actual_rgb_color[BLU]);
103 }
104 
105 static int
106 test_bu_color_to_rgb_floats(int argc, char *argv[])
107 {
108  fastf_t expected_rgb_color[3];
109  fastf_t actual_rgb_color[3];
110  struct bu_color color = BU_COLOR_INIT_ZERO;
111 
112  if (argc != 3) {
113  bu_exit(1, "ERROR: input format is rgb_color [%s]\n", argv[0]);
114  }
115 
116  sscanf(argv[2], "%lf,%lf,%lf", &expected_rgb_color[RED], &expected_rgb_color[GRN], &expected_rgb_color[BLU]);
117 
118  color.buc_rgb[RED] = expected_rgb_color[RED];
119  color.buc_rgb[GRN] = expected_rgb_color[GRN];
120  color.buc_rgb[BLU] = expected_rgb_color[BLU];
121 
122  bu_color_to_rgb_floats(&color, actual_rgb_color);
123 
124  printf("Result: %f,%f,%f", actual_rgb_color[RED], actual_rgb_color[GRN], actual_rgb_color[BLU]);
125 
126  return !(EQUAL(expected_rgb_color[RED], actual_rgb_color[RED])
127  && EQUAL(expected_rgb_color[GRN], actual_rgb_color[GRN])
128  && EQUAL(expected_rgb_color[BLU], actual_rgb_color[BLU]));
129 }
130 
131 static int
132 test_bu_color_from_rgb_floats(int argc, char *argv[])
133 {
134  fastf_t expected_rgb_color[3];
135  fastf_t actual_rgb_color[3];
136  struct bu_color color = BU_COLOR_INIT_ZERO;
137 
138  if (argc != 3) {
139  bu_exit(1, "ERROR: input format is rgb_color [%s]\n", argv[0]);
140  }
141 
142  sscanf(argv[2], "%lf,%lf,%lf", &expected_rgb_color[RED], &expected_rgb_color[GRN], &expected_rgb_color[BLU]);
143 
144  bu_color_from_rgb_floats(&color, expected_rgb_color);
145 
146  actual_rgb_color[RED] = color.buc_rgb[RED];
147  actual_rgb_color[GRN] = color.buc_rgb[GRN];
148  actual_rgb_color[BLU] = color.buc_rgb[BLU];
149 
150  printf("Result: %f,%f,%f", actual_rgb_color[RED], actual_rgb_color[GRN], actual_rgb_color[BLU]);
151 
152  return !(EQUAL(expected_rgb_color[RED], actual_rgb_color[RED])
153  && EQUAL(expected_rgb_color[GRN], actual_rgb_color[GRN])
154  && EQUAL(expected_rgb_color[BLU], actual_rgb_color[BLU]));
155 }
156 
157 int
158 main(int argc, char *argv[])
159 {
160  int function_num = 0;
161 
162  if (argc < 2) {
163  bu_exit(1, "ERROR: input format is function_num function_test_args [%s]\n", argv[0]);
164  }
165 
166  sscanf(argv[1], "%d", &function_num);
167 
168  switch (function_num) {
169  case 1:
170  return test_bu_rgb_to_hsv(argc, argv);
171  case 2:
172  return test_bu_hsv_to_rgb(argc, argv);
173  case 3:
174  return test_bu_str_to_rgb(argc, argv);
175  case 4:
176  return test_bu_color_to_rgb_floats(argc, argv);
177  case 5:
178  return test_bu_color_from_rgb_floats(argc, argv);
179  }
180 
181  bu_log("ERROR: function_num %d is not valid [%s]\n", function_num, argv[0]);
182  return 1;
183 }
184 
185 
186 /*
187  * Local Variables:
188  * mode: C
189  * tab-width: 8
190  * indent-tabs-mode: t
191  * c-file-style: "stroustrup"
192  * End:
193  * ex: shiftwidth=4 tabstop=8
194  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int bu_hsv_to_rgb(fastf_t *hsv, unsigned char *rgb)
Definition: color.c:121
int bu_str_to_rgb(char *str, unsigned char *rgb)
Definition: color.c:178
fastf_t buc_rgb[3]
Definition: color.h:52
#define RED
Definition: color.h:39
#define SAT
Definition: color.h:44
Header file for the BRL-CAD common definitions.
#define BLU
Definition: color.h:41
void bu_rgb_to_hsv(unsigned char *rgb, fastf_t *hsv)
Definition: color.c:60
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
int bu_color_to_rgb_floats(struct bu_color *cp, fastf_t *rgb)
Definition: color.c:220
int bu_color_from_rgb_floats(struct bu_color *cp, fastf_t *rgb)
Definition: color.c:235
#define HUE
Definition: color.h:43
#define BU_COLOR_INIT_ZERO
Definition: color.h:74
#define VAL
Definition: color.h:45
int main(int argc, char *argv[])
Definition: bu_color.c:158
#define GRN
Definition: color.h:40
double fastf_t
Definition: defines.h:300
Definition: color.h:49