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, unsigned int r, unsigned int g, unsigned int b)
32 c->f_red = ((double)r) / 65535;
33 c->f_green = ((double)g) / 65535;
34 c->f_blue = ((double)b) / 65535;
37 void color_set_f(struct color *c, double r, double g, double b)
48 struct color *color_new(unsigned int red, unsigned int green, unsigned int blue)
50 struct color *color = malloc(sizeof(struct color));
52 color_set(color, red, green, blue);
57 struct color *color_dup(struct color *color)
59 return color_new(color->red, color->green, color->blue);
62 int is_color(const char *str)
67 if (n != 13 || str[0] != '#')
70 for (i = 1; i < n; i++)
71 if (isxdigit(str[i]) == 0)
77 struct color *str_to_color(const char *str)
80 unsigned int red, green, blue;
85 strncpy(tmp, str + 1, 4);
87 red = strtol(tmp, NULL, 16);
89 strncpy(tmp, str + 5, 4);
91 green = strtol(tmp, NULL, 16);
93 strncpy(tmp, str + 9, 4);
95 blue = strtol(tmp, NULL, 16);
97 return color_new(red, green, blue);
100 char *color_to_str(const struct color *color)
102 char *str = malloc(1 + 12 + 1);
104 sprintf(str, "#%.4x%.4x%.4x", color->red, color->green, color->blue);