Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
15 KiB

  1. HRESULT WINAPI DXColorFromBSTR( BSTR Color, DWORD *pdwColor );
  2. typedef struct _COLORVALUE_PAIR
  3. {
  4. const CHAR *szName;
  5. DWORD dwValue;
  6. } COLORVALUE_PAIR;
  7. const COLORVALUE_PAIR
  8. rgColorNames[] =
  9. {
  10. { ("aliceblue"), 0xfff0f8ff },
  11. { ("antiquewhite"), 0xfffaebd7 },
  12. { ("aqua"), 0xff00ffff },
  13. { ("aquamarine"), 0xff7fffd4 },
  14. { ("azure"), 0xfff0ffff },
  15. { ("beige"), 0xfff5f5dc },
  16. { ("bisque"), 0xffffe4c4 },
  17. { ("black"), 0xff000000 },
  18. { ("blanchedalmond"), 0xffffebcd },
  19. { ("blue"), 0xff0000ff },
  20. { ("blueviolet"), 0xff8a2be2 },
  21. { ("brown"), 0xffa52a2a },
  22. { ("burlywood"), 0xffdeb887 },
  23. { ("cadetblue"), 0xff5f9ea0 },
  24. { ("chartreuse"), 0xff7fff00 },
  25. { ("chocolate"), 0xffd2691e },
  26. { ("coral"), 0xffff7f50 },
  27. { ("cornflowerblue"), 0xff6495ed },
  28. { ("cornsilk"), 0xfffff8dc },
  29. { ("crimson"), 0xffdc143c },
  30. { ("cyan"), 0xff00ffff },
  31. { ("darkblue"), 0xff00008b },
  32. { ("darkcyan"), 0xff008b8b },
  33. { ("darkgoldenrod"), 0xffb8860b },
  34. { ("darkgray"), 0xffa9a9a9 },
  35. { ("darkgreen"), 0xff006400 },
  36. { ("darkkhaki"), 0xffbdb76b },
  37. { ("darkmagenta"), 0xff8b008b },
  38. { ("darkolivegreen"), 0xff556b2f },
  39. { ("darkorange"), 0xffff8c00 },
  40. { ("darkorchid"), 0xff9932cc },
  41. { ("darkred"), 0xff8b0000 },
  42. { ("darksalmon"), 0xffe9967a },
  43. { ("darkseagreen"), 0xff8fbc8f },
  44. { ("darkslateblue"), 0xff483d8b },
  45. { ("darkslategray"), 0xff2f4f4f },
  46. { ("darkturquoise"), 0xff00ced1 },
  47. { ("darkviolet"), 0xff9400d3 },
  48. { ("deeppink"), 0xffff1493 },
  49. { ("deepskyblue"), 0xff00bfff },
  50. { ("dimgray"), 0xff696969 },
  51. { ("dodgerblue"), 0xff1e90ff },
  52. { ("firebrick"), 0xffb22222 },
  53. { ("floralwhite"), 0xfffffaf0 },
  54. { ("forestgreen"), 0xff228b22 },
  55. { ("fuchsia"), 0xffff00ff },
  56. { ("gainsboro"), 0xffdcdcdc },
  57. { ("ghostwhite"), 0xfff8f8ff },
  58. { ("gold"), 0xffffd700 },
  59. { ("goldenrod"), 0xffdaa520 },
  60. { ("gray"), 0xff808080 },
  61. { ("green"), 0xff008000 },
  62. { ("greenyellow"), 0xffadff2f },
  63. { ("honeydew"), 0xfff0fff0 },
  64. { ("hotpink"), 0xffff69b4 },
  65. { ("indianred"), 0xffcd5c5c },
  66. { ("indigo"), 0xff4b0082 },
  67. { ("ivory"), 0xfffffff0 },
  68. { ("khaki"), 0xfff0e68c },
  69. { ("lavender"), 0xffe6e6fa },
  70. { ("lavenderblush"), 0xfffff0f5 },
  71. { ("lawngreen"), 0xff7cfc00 },
  72. { ("lemonchiffon"), 0xfffffacd },
  73. { ("lightblue"), 0xffadd8e6 },
  74. { ("lightcoral"), 0xfff08080 },
  75. { ("lightcyan"), 0xffe0ffff },
  76. { ("lightgoldenrodyellow"), 0xfffafad2 },
  77. { ("lightgreen"), 0xff90ee90 },
  78. { ("lightgrey"), 0xffd3d3d3 },
  79. { ("lightpink"), 0xffffb6c1 },
  80. { ("lightsalmon"), 0xffffa07a },
  81. { ("lightseagreen"), 0xff20b2aa },
  82. { ("lightskyblue"), 0xff87cefa },
  83. { ("lightslategray"), 0xff778899 },
  84. { ("lightsteelblue"), 0xffb0c4de },
  85. { ("lightyellow"), 0xffffffe0 },
  86. { ("lime"), 0xff00ff00 },
  87. { ("limegreen"), 0xff32cd32 },
  88. { ("linen"), 0xfffaf0e6 },
  89. { ("magenta"), 0xffff00ff },
  90. { ("maroon"), 0xff800000 },
  91. { ("mediumaquamarine"), 0xff66cdaa },
  92. { ("mediumblue"), 0xff0000cd },
  93. { ("mediumorchid"), 0xffba55d3 },
  94. { ("mediumpurple"), 0xff9370db },
  95. { ("mediumseagreen"), 0xff3cb371 },
  96. { ("mediumslateblue"), 0xff7b68ee },
  97. { ("mediumspringgreen"), 0xff00fa9a },
  98. { ("mediumturquoise"), 0xff48d1cc },
  99. { ("mediumvioletred"), 0xffc71585 },
  100. { ("midnightblue"), 0xff191970 },
  101. { ("mintcream"), 0xfff5fffa },
  102. { ("mistyrose"), 0xffffe4e1 },
  103. { ("moccasin"), 0xffffe4b5 },
  104. { ("navajowhite"), 0xffffdead },
  105. { ("navy"), 0xff000080 },
  106. { ("oldlace"), 0xfffdf5e6 },
  107. { ("olive"), 0xff808000 },
  108. { ("olivedrab"), 0xff6b8e23 },
  109. { ("orange"), 0xffffa500 },
  110. { ("orangered"), 0xffff4500 },
  111. { ("orchid"), 0xffda70d6 },
  112. { ("palegoldenrod"), 0xffeee8aa },
  113. { ("palegreen"), 0xff98fb98 },
  114. { ("paleturquoise"), 0xffafeeee },
  115. { ("palevioletred"), 0xffdb7093 },
  116. { ("papayawhip"), 0xffffefd5 },
  117. { ("peachpuff"), 0xffffdab9 },
  118. { ("peru"), 0xffcd853f },
  119. { ("pink"), 0xffffc0cb },
  120. { ("plum"), 0xffdda0dd },
  121. { ("powderblue"), 0xffb0e0e6 },
  122. { ("purple"), 0xff800080 },
  123. { ("red"), 0xffff0000 },
  124. { ("rosybrown"), 0xffbc8f8f },
  125. { ("royalblue"), 0xff4169e1 },
  126. { ("saddlebrown"), 0xff8b4513 },
  127. { ("salmon"), 0xfffa8072 },
  128. { ("sandybrown"), 0xfff4a460 },
  129. { ("seagreen"), 0xff2e8b57 },
  130. { ("seashell"), 0xfffff5ee },
  131. { ("sienna"), 0xffa0522d },
  132. { ("silver"), 0xffc0c0c0 },
  133. { ("skyblue"), 0xff87ceeb },
  134. { ("slateblue"), 0xff6a5acd },
  135. { ("slategray"), 0xff708090 },
  136. { ("snow"), 0xfffffafa },
  137. { ("springgreen"), 0xff00ff7f },
  138. { ("steelblue"), 0xff4682b4 },
  139. { ("tan"), 0xffd2b48c },
  140. { ("teal"), 0xff008080 },
  141. { ("thistle"), 0xffd8bfd8 },
  142. { ("tomato"), 0xffff6347 },
  143. { ("turquoise"), 0xff40e0d0 },
  144. { ("violet"), 0xffee82ee },
  145. { ("wheat"), 0xfff5deb3 },
  146. { ("white"), 0xffffffff },
  147. { ("whitesmoke"), 0xfff5f5f5 },
  148. { ("yellow"), 0xffffff00 },
  149. { ("yellowgreen"), 0xff9acd32 }
  150. }; // rgColorNames[]
  151. #define SIZE_OF_COLOR_TABLE (sizeof(rgColorNames) / sizeof(COLORVALUE_PAIR))
  152. const COLORVALUE_PAIR
  153. rgSystemColorNames[] =
  154. {
  155. { ("activeborder"), COLOR_ACTIVEBORDER}, // Active window border.
  156. { ("activecaption"), COLOR_ACTIVECAPTION}, // Active window caption.
  157. { ("appworkspace"), COLOR_APPWORKSPACE}, // Background color of multiple document interface (MDI) applications.
  158. { ("background"), COLOR_BACKGROUND}, // Desktop background.
  159. { ("buttonface"), COLOR_BTNFACE}, // Face color for three-dimensional display elements.
  160. { ("buttonhighlight"), COLOR_BTNHIGHLIGHT}, // Dark shadow for three-dimensional display elements.
  161. { ("buttonshadow"), COLOR_BTNSHADOW}, // Shadow color for three-dimensional display elements (for edges facing away from the light source).
  162. { ("buttontext"), COLOR_BTNTEXT}, // Text on push buttons.
  163. { ("captiontext"), COLOR_CAPTIONTEXT}, // Text in caption, size box, and scroll bar arrow box.
  164. { ("graytext"), COLOR_GRAYTEXT}, // Grayed (disabled) text. This color is set to 0 if the current display driver does not support a solid gray color.
  165. { ("highlight"), COLOR_HIGHLIGHT}, // Item(s) selected in a control.
  166. { ("highlighttext"), COLOR_HIGHLIGHTTEXT}, // Text of item(s) selected in a control.
  167. { ("inactiveborder"), COLOR_INACTIVEBORDER}, // Inactive window border.
  168. { ("inactivecaption"), COLOR_INACTIVECAPTION}, // Inactive window caption.
  169. { ("inactivecaptiontext"),COLOR_INACTIVECAPTIONTEXT}, // Color of text in an inactive caption.
  170. { ("infobackground"), COLOR_INFOBK}, // Background color for tooltip controls.
  171. { ("infotext"), COLOR_INFOTEXT}, // Text color for tooltip controls.
  172. { ("menu"), COLOR_MENU}, // Menu background.
  173. { ("menutext"), COLOR_MENUTEXT}, // Text in menus.
  174. { ("scrollbar"), COLOR_SCROLLBAR}, // Scroll bar gray area.
  175. { ("threeddarkshadow"), COLOR_3DDKSHADOW }, // Dark shadow for three-dimensional display elements.
  176. { ("threedface"), COLOR_3DFACE},
  177. { ("threedhighlight"), COLOR_3DHIGHLIGHT}, // Highlight color for three-dimensional display elements (for edges facing the light source.)
  178. { ("threedlightshadow"), COLOR_3DLIGHT}, // Light color for three-dimensional display elements (for edges facing the light source.)
  179. { ("threedshadow"), COLOR_3DSHADOW}, // Dark shadow for three-dimensional display elements.
  180. { ("window"), COLOR_WINDOW}, // Window background.
  181. { ("windowframe"), COLOR_WINDOWFRAME}, // Window frame.
  182. { ("windowtext"), COLOR_WINDOWTEXT}, // Text in windows.
  183. }; // rgSystemColorNames[]
  184. #define SIZE_OF_SYSTEM_COLOR_TABLE (sizeof(rgSystemColorNames) / sizeof(COLORVALUE_PAIR))
  185. //
  186. // Used by DXColorFromBSTR to parse the hex string.
  187. //
  188. inline HRESULT _HexStrToDWORD( char * pch, DWORD * pdwColor )
  189. {
  190. HRESULT hr = S_OK;
  191. short hexVals[8];
  192. int numVals;
  193. int i;
  194. DWORD dwmult;
  195. if( !pch || !pdwColor )
  196. {
  197. hr = E_FAIL;
  198. }
  199. else
  200. {
  201. //--- parse input string
  202. numVals = 0;
  203. while( *pch && numVals < 8 )
  204. {
  205. if( *pch >= '0' && *pch <= '9' )
  206. {
  207. hexVals[numVals++] = *pch++ - '0';
  208. }
  209. else if( *pch >= 'a' && *pch <= 'f' )
  210. {
  211. hexVals[numVals++] = *pch++ - 'a' + 10;
  212. }
  213. else if( *pch >= 'A' && *pch <= 'F' )
  214. {
  215. hexVals[numVals++] = *pch++ - 'A' + 10;
  216. }
  217. else
  218. {
  219. break;
  220. }
  221. }
  222. if( numVals < 6 )
  223. {
  224. hr = E_FAIL;
  225. }
  226. else
  227. {
  228. //--- if we were passed 7 hex digits, only use the first 6
  229. if( numVals == 7 )
  230. {
  231. numVals = 6;
  232. }
  233. //--- initialize multiplier
  234. dwmult = 1;
  235. *pdwColor = 0;
  236. //--- multiply hexVals by 16^i
  237. for( i = 0; i < numVals; i++ )
  238. {
  239. *pdwColor += (hexVals[numVals-(i+1)]*dwmult);
  240. dwmult <<= 4;
  241. }
  242. }
  243. }
  244. return hr;
  245. }
  246. //
  247. // Used by DXColorFromBSTR to convert a string to lower case
  248. //
  249. inline void StringToLowerCase( CHAR * pstrKey )
  250. {
  251. if( pstrKey )
  252. {
  253. while( *pstrKey )
  254. {
  255. if( *pstrKey >= 'A' && *pstrKey <= 'Z' )
  256. {
  257. *pstrKey = 'a' + *pstrKey - 'A';
  258. }
  259. pstrKey++;
  260. }
  261. }
  262. }
  263. inline HRESULT WINAPI DXColorFromBSTR( BSTR Color, DWORD *pdwColor )
  264. {
  265. HRESULT hr = S_OK;
  266. // validate arguments
  267. int strLen = wcslen(Color);
  268. if( strLen < 3 || DXIsBadReadPtr( Color, strLen ) ||
  269. DXIsBadWritePtr( pdwColor, sizeof(*pdwColor) ) )
  270. {
  271. hr = E_INVALIDARG;
  272. }
  273. else
  274. {
  275. //--- first check for #AARRGGBB and #RRGGBB strings
  276. if( Color[0] == L'#' )
  277. {
  278. CHAR ch[11] = "0xffffffff";
  279. if( strLen > 9 ) strLen = 9;
  280. if( strLen >= 7 )
  281. {
  282. WideCharToMultiByte( CP_ACP, NULL, &Color[1], strLen-1, &ch[11-strLen], strLen, NULL, NULL );
  283. hr = _HexStrToDWORD( &ch[2], pdwColor);
  284. }
  285. else
  286. {
  287. hr = E_FAIL;
  288. }
  289. }
  290. else
  291. {
  292. //--- convert the string to lower case
  293. LPSTR pstrKey = (LPSTR) alloca((strLen + 1) * sizeof(CHAR));
  294. WideCharToMultiByte( CP_ACP, NULL, Color, strLen+1, pstrKey, strLen+1, NULL, NULL );
  295. StringToLowerCase( pstrKey );
  296. //--- search the string table using a binary search
  297. int nFirst = 0;
  298. int nLast = SIZE_OF_COLOR_TABLE - 1;
  299. int nCur, nCmp;
  300. bool bFound = false;
  301. while( (nFirst <= nLast) && !bFound )
  302. {
  303. nCur = (nFirst + nLast) / 2;
  304. nCmp = strcmp( rgColorNames[nCur].szName, pstrKey );
  305. if( nCmp < 0 )
  306. {
  307. nFirst = nCur + 1;
  308. }
  309. else if( nCmp > 0 )
  310. {
  311. nLast = nCur - 1;
  312. }
  313. else
  314. {
  315. bFound = true;
  316. *pdwColor = rgColorNames[nCur].dwValue;
  317. }
  318. }
  319. // String not in regular color table, try the system color table.
  320. if (!bFound)
  321. {
  322. nFirst = 0;
  323. nLast = SIZE_OF_SYSTEM_COLOR_TABLE - 1;
  324. while ((nFirst <= nLast) && !bFound)
  325. {
  326. nCur = (nFirst + nLast) / 2;
  327. nCmp = strcmp(rgSystemColorNames[nCur].szName, pstrKey);
  328. if (nCmp < 0)
  329. {
  330. nFirst = nCur + 1;
  331. }
  332. else if (nCmp > 0)
  333. {
  334. nLast = nCur - 1;
  335. }
  336. else
  337. {
  338. bFound = true;
  339. *pdwColor = ::GetSysColor((int)rgSystemColorNames[nCur].dwValue);
  340. *pdwColor |= 0xFF000000;
  341. }
  342. }
  343. // String not found in regular or system color table.
  344. if (!bFound)
  345. {
  346. hr = E_FAIL;
  347. }
  348. }
  349. }
  350. }
  351. return hr;
  352. }