2 * Copyright (C) 2010-2013 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 *color,
35 color->f_red = ((double)color->red) / 65535;
36 color->f_green = ((double)color->green) / 65535;
37 color->f_blue = ((double)color->blue) / 65535;
40 struct color *color_new(unsigned int red, unsigned int green, unsigned int blue)
42 struct color *color = malloc(sizeof(struct color));
44 color_set(color, red, green, blue);
49 struct color *color_dup(struct color *color)
51 return color_new(color->red, color->green, color->blue);
54 int is_color(const char *str)
59 if (n != 13 || str[0] != '#')
62 for (i = 1; i < n; i++)
63 if (isxdigit(str[i]) == 0)
69 struct color *str_to_color(const char *str)
72 unsigned int red, green, blue;
77 strncpy(tmp, str + 1, 4);
79 red = strtol(tmp, NULL, 16);
81 strncpy(tmp, str + 5, 4);
83 green = strtol(tmp, NULL, 16);
85 strncpy(tmp, str + 9, 4);
87 blue = strtol(tmp, NULL, 16);
89 return color_new(red, green, blue);
92 char *color_to_str(const struct color *color)
94 char *str = malloc(1 + 12 + 1);
96 sprintf(str, "#%.4x%.4x%.4x", color->red, color->green, color->blue);