Browse Source

add hex color parsing

master
Alexandros Theodotou 3 years ago
parent
commit
8c6c2e7500
Signed by: alex
GPG Key ID: 022EAE42313D70F3
  1. 20
      inc/ztoolkit/log.h
  2. 8
      inc/ztoolkit/ztk_color.h
  3. 32
      src/ztk_color.c
  4. 61
      tests/color.c
  5. 8
      tests/log.c
  6. 10
      tests/meson.build

20
inc/ztoolkit/log.h

@ -60,16 +60,20 @@ ztk_log ( @@ -60,16 +60,20 @@ ztk_log (
const char * format,
...);
#define ztk_debug(msg) \
ztk_log (__func__, ZTK_LOG_LEVEL_DEBUG, msg)
#define ztk_debug(format,...) \
ztk_log (__func__, ZTK_LOG_LEVEL_DEBUG, \
format, __VA_ARGS__)
#define ztk_message(msg) \
ztk_log (__func__, ZTK_LOG_LEVEL_MESSAGE, msg)
#define ztk_message(format,...) \
ztk_log (__func__, ZTK_LOG_LEVEL_MESSAGE, \
format, __VA_ARGS__)
#define ztk_warning(msg) \
ztk_log (__func__, ZTK_LOG_LEVEL_WARNING, msg)
#define ztk_warning(format,...) \
ztk_log (__func__, ZTK_LOG_LEVEL_WARNING, \
format, __VA_ARGS__)
#define ztk_error(msg) \
ztk_log (__func__, ZTK_LOG_LEVEL_ERROR, msg)
#define ztk_error(format,...) \
ztk_log (__func__, ZTK_LOG_LEVEL_ERROR, format, \
__VA_ARGS__)
#endif

8
inc/ztoolkit/ztk_color.h

@ -38,4 +38,12 @@ ztk_color_set_for_cairo ( @@ -38,4 +38,12 @@ ztk_color_set_for_cairo (
ZtkColor * color,
cairo_t * cr);
/**
* Parses a ZtkColor from the given hex string.
*/
void
ztk_color_parse_hex (
ZtkColor * color,
const char * hex_str);
#endif

32
src/ztk_color.c

@ -17,8 +17,11 @@ @@ -17,8 +17,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include "ztoolkit/ztk_color.h"
#include <ztoolkit/log.h>
#include <ztoolkit/ztk_color.h>
void
ztk_color_set_for_cairo (
@ -29,3 +32,30 @@ ztk_color_set_for_cairo ( @@ -29,3 +32,30 @@ ztk_color_set_for_cairo (
cr, color->red, color->green, color->blue,
color->alpha);
}
/**
* Parses a ZtkColor from the given hex string.
*/
void
ztk_color_parse_hex (
ZtkColor * color,
const char * hex_str)
{
char str[3];
long num;
str[0] = hex_str[1];
str[1] = hex_str[2];
str[2] = '\0';
num = strtol (str, NULL, 16);
color->red = (double) num / 255;
str[0] = hex_str[3];
str[1] = hex_str[4];
str[2] = '\0';
num = strtol (str, NULL, 16);
color->green = (double) num / 255;
str[0] = hex_str[5];
str[1] = hex_str[6];
str[2] = '\0';
num = strtol (str, NULL, 16);
color->blue = (double) num / 255;
}

61
tests/color.c

@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZToolkit
*
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
#include <float.h>
#include "helper.h"
#include <ztoolkit/ztk_color.h>
#define doubles_equal(a,b) \
((a) > (b) ? \
(a) - (b) < DBL_EPSILON : \
(b) - (a) < DBL_EPSILON)
/* for debugging */
#define print_color(cl) \
ztk_error ("red %f", color.red); \
ztk_error ("green %f", color.green); \
ztk_error ("blue %f", color.blue)
int main (
int argc, const char* argv[])
{
ZtkColor color;
ztk_color_parse_hex (&color, "#000000");
ztk_assert (doubles_equal (color.red, 0.0));
ztk_assert (doubles_equal (color.green, 0.0));
ztk_assert (doubles_equal (color.blue, 0.0));
ztk_color_parse_hex (&color, "#FFFFFF");
ztk_assert (doubles_equal (color.red, 1.0));
ztk_assert (doubles_equal (color.green, 1.0));
ztk_assert (doubles_equal (color.blue, 1.0));
ztk_color_parse_hex (&color, "#AA234F");
ztk_assert (
doubles_equal (color.red, 170 / 255.0));
ztk_assert (
doubles_equal (color.green, 35.0 / 255.0));
ztk_assert (
doubles_equal (color.blue, 79.0 / 255.0));
return 0;
}

8
tests/log.c

@ -22,10 +22,10 @@ @@ -22,10 +22,10 @@
int main (
int argc, const char* argv[])
{
ztk_debug ("debug test");
ztk_message ("message test");
ztk_warning ("warning test");
ztk_error ("error test");
ztk_debug ("%s", "debug test");
ztk_message ("%s", "message test");
ztk_warning ("%s", "warning test");
ztk_error ("%s", "error test");
return 0;
}

10
tests/meson.build

@ -16,8 +16,16 @@ @@ -16,8 +16,16 @@
# along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
e = executable (
'log_exe', 'log.c',
'log', 'log.c',
include_directories: inc_dirs,
link_with: ztoolkit_lib,
)
test ('log_test', e)
e = executable (
'color', 'color.c',
include_directories: inc_dirs,
link_with: ztoolkit_lib,
dependencies: deps,
)
test ('color_test', e)

Loading…
Cancel
Save