2 * Copyright (c) 2007, 2008, 2009, Czirkos Zoltan <cirix@fw.hu>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 static char *c64_color_names[] =
40 /* return c64 color with index. */
41 GdColor gd_c64_color(int index)
43 return (GD_COLOR_TYPE_C64 << 24) + index;
46 /* return atari color with index. */
47 GdColor gd_atari_color(int index)
49 return (GD_COLOR_TYPE_ATARI << 24) + index;
52 /* return c64dtv color with index. */
53 GdColor gd_c64dtv_color(int index)
55 return (GD_COLOR_TYPE_C64DTV << 24) + index;
58 /* return "unknown color" */
59 static GdColor unknown_color(void)
61 return (GD_COLOR_TYPE_UNKNOWN << 24);
64 /* make up GdColor from r,g,b values. */
65 GdColor gd_color_get_from_rgb(int r, int g, int b)
67 return (GD_COLOR_TYPE_RGB << 24) + (r << 16) + (g << 8) + b;
70 GdColor gd_color_get_from_string(const char *color)
74 for (i = 0; i < ARRAY_SIZE(c64_color_names); i++)
75 if (strEqualCase(color, c64_color_names[i]))
76 return gd_c64_color(i);
78 /* we do not use sscanf(color, "atari..." as may be lowercase */
79 if (strEqualCaseN(color, "Atari", strlen("Atari")))
81 const char *b = color + strlen("Atari");
84 if (sscanf(b, "%02x", &c) == 1)
85 return gd_atari_color(c);
87 Warn("Unknown Atari color: %s", color);
89 return unknown_color();
92 /* we do not use sscanf(color, "c64dtv..." as may be lowercase */
93 if (strEqualCaseN(color, "C64DTV", strlen("C64DTV")))
95 const char *b = color + strlen("C64DTV");
98 if (sscanf(b, "%02x", &c) == 1)
99 return gd_c64dtv_color(c);
101 Warn("Unknown C64DTV color: %s", color);
103 return unknown_color();
106 /* may or may not have a # */
110 if (sscanf(color, "%02x%02x%02x", &r, &g, &b) != 3)
112 i = gd_random_int_range(0, 16);
114 Warn("Unkonwn color %s", color);
116 return unknown_color();
119 return gd_color_get_from_rgb(r, g, b);
122 const char *gd_color_get_string(GdColor color)
124 static char text[16];
126 if (gd_color_is_c64(color))
128 return c64_color_names[color & 0xff];
131 if (gd_color_is_atari(color))
133 sprintf(text, "Atari%02x", color & 0xff);
137 if (gd_color_is_dtv(color))
139 sprintf(text, "C64DTV%02x", color & 0xff);
143 sprintf(text, "#%02x%02x%02x", (color >> 16) & 255, (color >> 8) & 255, color & 255);
147 boolean gd_color_is_c64(GdColor color)
149 return (color >> 24) == GD_COLOR_TYPE_C64;
152 boolean gd_color_is_atari(GdColor color)
154 return (color >> 24) == GD_COLOR_TYPE_ATARI;
157 boolean gd_color_is_dtv(GdColor color)
159 return (color >> 24) == GD_COLOR_TYPE_C64DTV;
162 boolean gd_color_is_unknown(GdColor color)
164 return (color >> 24) == GD_COLOR_TYPE_UNKNOWN;
167 GdColor gd_gdash_color(int c)
169 /* these values are taken from the title screen, drawn by cws. */
170 /* so menus and everything else will look nice! */
171 /* the 16 colors that can be used are the same as on c64. */
172 /* "Black", "White", "Red", "Cyan", "Purple", "Green", "Blue", "Yellow", */
173 /* "Orange", "Brown", "LightRed", "Gray1", "Gray2", "LightGreen", "LightBlue", "Gray3", */
174 /* not in the png: cyan, purple. gray3 is darker in the png. */
175 /* 17th color is the player's leg in the png. i not connected it to any c64 */
176 /* color, but it is used for theme images for example. */
177 const GdColor gdash_colors[] =
179 0x000000, 0xffffff, 0xe33939, 0x55aaaa, 0xaa55aa, 0x71aa55, 0x0039ff, 0xffff55,
180 0xe37139, 0xaa7139, 0xe09080, 0x555555, 0x717171, 0xc6e38e, 0xaaaaff, 0x8e8e8e,
185 return gdash_colors[c];