2 * Copyright 2010-2013, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Stefano Ceccherini, stefano.ceccherini@gmail.com
7 * Siarzhuk Zharski, zharik@gmx.li
17 #include <Application.h>
19 #include <Resources.h>
22 #undef B_TRANSLATION_CONTEXT
23 #define B_TRANSLATION_CONTEXT "Terminal colors scheme"
27 const rgb_color kBlack
= { 0, 0, 0, 255 };
28 const rgb_color kGreen
= { 0, 255, 0, 255 };
29 const rgb_color kWhite
= { 255, 255, 255, 255 };
30 const rgb_color kYellow
= { 255, 255, 0, 255 };
33 const struct color_scheme kColorSchemeDefault
= {
34 B_TRANSLATE("Default"),
43 const struct color_scheme kColorSchemeBlue
= {
53 const struct color_scheme kColorSchemeMidnight
= {
54 B_TRANSLATE("Midnight"),
63 const struct color_scheme kColorSchemeProfessional
= {
64 B_TRANSLATE("Professional"),
73 const struct color_scheme kColorSchemeRetro
= {
83 const struct color_scheme kColorSchemeSlate
= {
93 const struct color_scheme kColorSchemeSolarizedDark
= {
94 B_TRANSLATE("Solarized Dark"),
95 { 119, 137, 139, 255 },
98 { 120, 137, 139, 255 },
99 { 136, 151, 151, 255 },
103 const struct color_scheme kColorSchemeSolarizedLight
= {
104 B_TRANSLATE("Solarized Light"),
105 { 90, 112, 120, 255 },
106 { 253, 244, 223, 255 },
107 { 236, 228, 207, 255 },
108 { 90, 112, 120, 255 },
109 { 78, 99, 106, 255 },
110 { 236, 228, 207, 255 },
113 struct color_scheme gCustomColorScheme
= {
114 B_TRANSLATE("Custom")
117 const color_scheme
* gPredefinedColorSchemes
[] = {
118 &kColorSchemeDefault
,
120 &kColorSchemeMidnight
,
121 &kColorSchemeProfessional
,
124 &kColorSchemeSolarizedDark
,
125 &kColorSchemeSolarizedLight
,
132 color_scheme::operator==(const color_scheme
& scheme
)
134 return text_fore_color
== scheme
.text_fore_color
135 && text_back_color
== scheme
.text_back_color
136 && cursor_fore_color
== scheme
.cursor_fore_color
137 && cursor_back_color
== scheme
.cursor_back_color
138 && select_fore_color
== scheme
.select_fore_color
139 && select_back_color
== scheme
.select_back_color
;
142 // #pragma mark XColorsTable implementation
144 XColorsTable gXColorsTable
;
147 XColorsTable::XColorsTable()
155 XColorsTable::~XColorsTable()
157 // fTable as result of LoadResource call and owned
158 // by BApplication so no need to free it explicitly
165 XColorsTable::LookUpColor(const char* name
, rgb_color
* color
)
167 if (name
== NULL
|| color
== NULL
)
170 // first check for 'rgb:xxx/xxx/xxx'-encoded color names
171 const char magic
[5] = "rgb:";
172 if (strncasecmp(name
, magic
, sizeof(magic
) - 1) == 0) {
173 int r
= 0, g
= 0, b
= 0;
174 if (sscanf(&name
[4], "%x/%x/%x", &r
, &g
, &b
) == 3) {
175 color
->set_to(0xFF & r
, 0xFF & g
, 0xFF & b
);
178 // else - let the chance lookup in rgb.txt table. Is it reasonable??
181 // for non-'rgb:xxx/xxx/xxx'-encoded - search the X11 rgb table
182 if (fTable
== NULL
) {
183 status_t result
= _LoadXColorsTable();
188 // use binary search to lookup color name hash in table
191 uint32 hash
= _HashName(name
);
192 while ((right
- left
) > 1) {
193 int i
= (left
+ right
) / 2;
194 ((fTable
[i
].hash
< hash
) ? left
: right
) = i
;
197 if (fTable
[right
].hash
== hash
) {
198 memcpy(color
, &fTable
[right
].color
, sizeof(rgb_color
));
202 return B_NAME_NOT_FOUND
;
207 XColorsTable::_LoadXColorsTable()
209 BResources
* res
= BApplication::AppResources();
214 fTable
= (_XColorEntry
*)res
->LoadResource(B_RAW_TYPE
, "XColorsTable", &size
);
215 if (fTable
== NULL
|| size
< sizeof(_XColorEntry
)) {
217 return B_ENTRY_NOT_FOUND
;
220 fCount
= size
/ sizeof(_XColorEntry
);
226 XColorsTable::_HashName(const char* name
)
231 // Using modified P.J.Weinberger hash algorithm
232 // Spaces are purged out, characters are upper-cased
233 // 'E' replaced with 'A' (to join 'grey' and 'gray' variations)
234 for (const char* p
= name
; *p
; p
++) {
235 int ch
= toupper(*p
);
242 hash
= (hash
<< 4) + (ch
& 0xFF);
243 g
= hash
& 0xf0000000;