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.

99 lines
2.3 KiB

  1. // color.cpp : Implementation of color.h
  2. #include "priv.h"
  3. #include <shlwapi.h>
  4. #include <exdispid.h>
  5. #include <shguidp.h>
  6. #include <hlink.h>
  7. #include <color.h>
  8. COLORREF ColorRefFromHTMLColorStrA(LPCSTR pszColor)
  9. {
  10. WCHAR wzColor[MAX_COLOR_STR];
  11. SHAnsiToUnicode(pszColor, wzColor, ARRAYSIZE(wzColor));
  12. if (wzColor[0] == '#')
  13. return HashStrToColorRefW(wzColor);
  14. return ColorRefFromHTMLColorStrW(wzColor);
  15. }
  16. COLORREF ColorRefFromHTMLColorStrW(LPCWSTR pwzColor)
  17. {
  18. int min, max, i, cmp;
  19. if (pwzColor[0] == '#')
  20. return HashStrToColorRefW(pwzColor);
  21. // Look for in regular colors
  22. min = 0;
  23. max = NUM_HTML_COLORS-1;
  24. while (min <= max)
  25. {
  26. i = (min + max) / 2;
  27. cmp = StrCmpW(pwzColor, ColorNames[i].pwzColorName);
  28. if (cmp < 0)
  29. max = i-1;
  30. else if (cmp > 0)
  31. min = i+1;
  32. else return ColorNames[i].colorRef;
  33. }
  34. // Look for in system colors
  35. min = 0;
  36. max = NUM_SYS_COLORS-1;
  37. while (min <= max)
  38. {
  39. i = (min + max) / 2;
  40. cmp = StrCmpW(pwzColor, SysColorNames[i].pwzColorName);
  41. if (cmp < 0)
  42. max = i-1;
  43. else if (cmp > 0)
  44. min = i+1;
  45. else return GetSysColor(SysColorNames[i].colorIndex);
  46. }
  47. return 0xffffff; // return white as default color
  48. }
  49. COLORREF HashStrToColorRefW(LPCWSTR pwzHashStr)
  50. {
  51. DWORD retColor = 0;
  52. int numBytes = lstrlenW(pwzHashStr);
  53. DWORD thisByte;
  54. // don't look at the first character because you know its a #
  55. for (int i=0 ; i < numBytes-1 ; i++)
  56. {
  57. thisByte = HexCharToDWORDW(pwzHashStr[numBytes-i-1]);
  58. retColor |= thisByte << (i*4);
  59. }
  60. return (COLORREF)retColor;
  61. }
  62. COLORREF HashStrToColorRefA(LPCSTR pszHashStr)
  63. {
  64. WCHAR wzHashStr[MAX_COLOR_STR];
  65. SHAnsiToUnicode(pszHashStr, wzHashStr, ARRAYSIZE(wzHashStr));
  66. return HashStrToColorRefW(wzHashStr);
  67. }
  68. DWORD HexCharToDWORDW(WCHAR wcHexNum)
  69. {
  70. if ((wcHexNum >= '0') && (wcHexNum <= '9'))
  71. return (wcHexNum - '0');
  72. if ((wcHexNum >= 'a') && (wcHexNum <= 'f'))
  73. return (wcHexNum - 'a' + 10);
  74. if ((wcHexNum >= 'A') && (wcHexNum <= 'F'))
  75. return (wcHexNum - 'A' + 10);
  76. return 0;
  77. }