1 /* 2 * Copyright 2010-2013, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stefano Ceccherini, stefano.ceccherini@gmail.com 7 * Siarzhuk Zharski, zharik@gmx.li 8 */ 9 10 11 #include "Colors.h" 12 13 #include <ctype.h> 14 #include <stdio.h> 15 #include <strings.h> 16 17 #include <Application.h> 18 #include <Catalog.h> 19 #include <Resources.h> 20 21 22 #undef B_TRANSLATION_CONTEXT 23 #define B_TRANSLATION_CONTEXT "Terminal colors scheme" 24 25 26 // Standard colors 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 }; 31 32 33 const struct color_scheme kColorSchemeDefault = { 34 B_TRANSLATE("Default"), 35 kBlack, 36 kWhite, 37 kWhite, 38 kBlack, 39 kWhite, 40 kBlack 41 }; 42 43 const struct color_scheme kColorSchemeBlue = { 44 B_TRANSLATE("Blue"), 45 kYellow, 46 { 0, 0, 139, 255 }, 47 kBlack, 48 kYellow, 49 kBlack, 50 { 0, 139, 139, 255 }, 51 }; 52 53 const struct color_scheme kColorSchemeMidnight = { 54 B_TRANSLATE("Midnight"), 55 kWhite, 56 kBlack, 57 kBlack, 58 kWhite, 59 kBlack, 60 kWhite 61 }; 62 63 const struct color_scheme kColorSchemeProfessional = { 64 B_TRANSLATE("Professional"), 65 kWhite, 66 { 8, 8, 8, 255 }, 67 { 50, 50, 50, 255 }, 68 kWhite, 69 kWhite, 70 { 50, 50, 50, 255 }, 71 }; 72 73 const struct color_scheme kColorSchemeRetro = { 74 B_TRANSLATE("Retro"), 75 kGreen, 76 kBlack, 77 kBlack, 78 kGreen, 79 kBlack, 80 kGreen 81 }; 82 83 const struct color_scheme kColorSchemeSlate = { 84 B_TRANSLATE("Slate"), 85 kWhite, 86 { 20, 20, 28, 255 }, 87 { 70, 70, 70, 255 }, 88 { 255, 200, 0, 255 }, 89 kWhite, 90 { 70, 70, 70, 255 }, 91 }; 92 93 struct color_scheme gCustomColorScheme = { 94 B_TRANSLATE("Custom") 95 }; 96 97 const color_scheme* gPredefinedColorSchemes[] = { 98 &kColorSchemeDefault, 99 &kColorSchemeBlue, 100 &kColorSchemeMidnight, 101 &kColorSchemeProfessional, 102 &kColorSchemeRetro, 103 &kColorSchemeSlate, 104 &gCustomColorScheme, 105 NULL 106 }; 107 108 109 bool 110 color_scheme::operator==(const color_scheme& scheme) 111 { 112 return text_fore_color == scheme.text_fore_color 113 && text_back_color == scheme.text_back_color 114 && cursor_fore_color == scheme.cursor_fore_color 115 && cursor_back_color == scheme.cursor_back_color 116 && select_fore_color == scheme.select_fore_color 117 && select_back_color == scheme.select_back_color; 118 } 119 120 // #pragma mark XColorsTable implementation 121 122 XColorsTable gXColorsTable; 123 124 125 XColorsTable::XColorsTable() 126 : 127 fTable(NULL), 128 fCount(0) 129 { 130 } 131 132 133 XColorsTable::~XColorsTable() 134 { 135 // fTable as result of LoadResource call and owned 136 // by BApplication so no need to free it explicitly 137 fTable = NULL; 138 fCount = 0; 139 } 140 141 142 status_t 143 XColorsTable::LookUpColor(const char* name, rgb_color* color) 144 { 145 if (name == NULL || color == NULL) 146 return B_BAD_DATA; 147 148 // first check for 'rgb:xxx/xxx/xxx'-encoded color names 149 const char magic[5] = "rgb:"; 150 if (strncasecmp(name, magic, sizeof(magic) - 1) == 0) { 151 int r = 0, g = 0, b = 0; 152 if (sscanf(&name[4], "%x/%x/%x", &r, &g, &b) == 3) { 153 color->set_to(0xFF & r, 0xFF & g, 0xFF & b); 154 return B_OK; 155 } 156 // else - let the chance lookup in rgb.txt table. Is it reasonable?? 157 } 158 159 // for non-'rgb:xxx/xxx/xxx'-encoded - search the X11 rgb table 160 if (fTable == NULL) { 161 status_t result = _LoadXColorsTable(); 162 if (result != B_OK) 163 return result; 164 } 165 166 // use binary search to lookup color name hash in table 167 int left = -1; 168 int right = fCount; 169 uint32 hash = _HashName(name); 170 while ((right - left) > 1) { 171 int i = (left + right) / 2; 172 ((fTable[i].hash < hash) ? left : right) = i; 173 } 174 175 if (fTable[right].hash == hash) { 176 memcpy(color, &fTable[right].color, sizeof(rgb_color)); 177 return B_OK; 178 } 179 180 return B_NAME_NOT_FOUND; 181 } 182 183 184 status_t 185 XColorsTable::_LoadXColorsTable() 186 { 187 BResources* res = BApplication::AppResources(); 188 if (res == NULL) 189 return B_ERROR; 190 191 size_t size = 0; 192 fTable = (_XColorEntry*)res->LoadResource(B_RAW_TYPE, "XColorsTable", &size); 193 if (fTable == NULL || size < sizeof(_XColorEntry)) { 194 fTable = NULL; 195 return B_ENTRY_NOT_FOUND; 196 } 197 198 fCount = size / sizeof(_XColorEntry); 199 return B_OK; 200 } 201 202 203 uint32 204 XColorsTable::_HashName(const char* name) 205 { 206 uint32 hash = 0; 207 uint32 g = 0; 208 209 // Using modified P.J.Weinberger hash algorithm 210 // Spaces are purged out, characters are upper-cased 211 // 'E' replaced with 'A' (to join 'grey' and 'gray' variations) 212 for (const char* p = name; *p; p++) { 213 int ch = toupper(*p); 214 switch (ch) { 215 case ' ': 216 break; 217 case 'E': 218 ch = 'A'; 219 default: 220 hash = (hash << 4) + (ch & 0xFF); 221 g = hash & 0xf0000000; 222 if (g != 0) { 223 hash ^= g >> 24; 224 hash ^= g; 225 } 226 break; 227 } 228 } 229 230 return hash; 231 } 232