diff options
author | Matthias Clasen <mclasen@redhat.com> | 2023-05-12 18:47:21 -0400 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2023-05-12 19:52:42 -0400 |
commit | 25c4acb6d2d7603d2b5600d27c4bd0cd2a186b7e (patch) | |
tree | 26f9aef6734c70f5e231ce41bdb07a888d46e4ab | |
parent | 0a3dac10c6c9180c54e38025b4bcdd0a91597c80 (diff) | |
download | gtk+-25c4acb6d2d7603d2b5600d27c4bd0cd2a186b7e.tar.gz |
Add tests for hsv<>rgb conversion
-rw-r--r-- | testsuite/gtk/colorutils.c | 62 | ||||
-rw-r--r-- | testsuite/gtk/meson.build | 1 |
2 files changed, 63 insertions, 0 deletions
diff --git a/testsuite/gtk/colorutils.c b/testsuite/gtk/colorutils.c new file mode 100644 index 0000000000..b4d42e2bb2 --- /dev/null +++ b/testsuite/gtk/colorutils.c @@ -0,0 +1,62 @@ +/* Copyright (C) 2023 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see <http://www.gnu.org/licenses/>. + */ +#include <gtk/gtk.h> +#include <gtk/gtkcolorutils.h> + +struct { + float r, g, b; + float h, s, v; +} tests[] = { + { 0, 0, 0, 0, 0, 0 }, + { 1, 1, 1, 0, 0, 1 }, + { 1, 0, 0, 0, 1, 1 }, + { 1, 1, 0, 1.0 / 6.0, 1, 1 }, + { 0, 1, 0, 2.0 / 6.0, 1, 1 }, + { 0, 1, 1, 3.0 / 6.0, 1, 1 }, + { 0, 0, 1, 4.0 / 6.0, 1, 1 }, + { 1, 0, 1, 5.0 / 6.0, 1, 1 }, +}; + +static void +test_roundtrips (void) +{ + for (unsigned int i = 0; i < G_N_ELEMENTS (tests); i++) + { + float r, g, b; + float h, s, v; + + g_print ("color %u\n", i); + gtk_hsv_to_rgb (tests[i].h, tests[i].s, tests[i].v, &r, &g, &b); + g_assert_cmpfloat_with_epsilon (r, tests[i].r, FLT_EPSILON); + g_assert_cmpfloat_with_epsilon (g, tests[i].g, FLT_EPSILON); + g_assert_cmpfloat_with_epsilon (b, tests[i].b, FLT_EPSILON); + gtk_rgb_to_hsv (tests[i].r, tests[i].g, tests[i].b, &h, &s, &v); + g_assert_cmpfloat_with_epsilon (h, tests[i].h, FLT_EPSILON); + g_assert_cmpfloat_with_epsilon (s, tests[i].s, FLT_EPSILON); + g_assert_cmpfloat_with_epsilon (v, tests[i].v, FLT_EPSILON); + } +} + +int +main (int argc, + char *argv[]) +{ + gtk_test_init (&argc, &argv); + + g_test_add_func ("/color/roundtrips", test_roundtrips); + + return g_test_run(); +} diff --git a/testsuite/gtk/meson.build b/testsuite/gtk/meson.build index 7f28f213ec..0ee1403828 100644 --- a/testsuite/gtk/meson.build +++ b/testsuite/gtk/meson.build @@ -127,6 +127,7 @@ internal_tests = [ { 'name': 'fnmatch' }, { 'name': 'a11y' }, { 'name': 'listitemmanager' }, + { 'name': 'colorutils' }, ] is_debug = get_option('buildtype').startswith('debug') |