2 * Copyright (C) 2010-2014 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 void color_set(struct color *c, double r, double g, double b)
33 struct color *color_new(double r, double g, double b)
37 color = malloc(sizeof(struct color));
39 color_set(color, r, g, b);
44 struct color *color_dup(struct color *color)
46 return color_new(color->red, color->green, color->blue);
49 int is_color(const char *str)
54 if (n != 13 || str[0] != '#')
57 for (i = 1; i < n; i++)
58 if (isxdigit(str[i]) == 0)
64 struct color *str_to_color(const char *str)
67 unsigned int red, green, blue;
72 strncpy(tmp, str + 1, 4);
74 red = strtol(tmp, NULL, 16);
76 strncpy(tmp, str + 5, 4);
78 green = strtol(tmp, NULL, 16);
80 strncpy(tmp, str + 9, 4);
82 blue = strtol(tmp, NULL, 16);
84 return color_new(((double)red)/65535,
85 ((double)green)/65535,
86 ((double)blue)/65535);
89 char *color_to_str(const struct color *color)
91 char *str = malloc(1 + 12 + 1);
93 sprintf(str, "#%.4x%.4x%.4x",
94 (int)(65535 * color->red),
95 (int)(65535 * color->green),
96 (int)(65535 * color->blue));