| 1 |
unit HTMLColors; |
| 2 |
|
| 3 |
{ Compiling this causes a number of unavoidable FPC notes, |
| 4 |
|
| 5 |
Note: (6058) Call to subroutine "operator =(const op1:Variant;const op2:Variant):Boolean;" marked as inline is not inlined |
| 6 |
.... } |
| 7 |
{$notes off} |
| 8 |
|
| 9 |
interface |
| 10 |
|
| 11 |
uses |
| 12 |
SysUtils, LCLIntf, Graphics, Variants; |
| 13 |
|
| 14 |
const |
| 15 |
SPECIAL_COUNT = 140; |
| 16 |
WEBSAFE_COUNT = 216; |
| 17 |
SYSTEM_COUNT = 28; |
| 18 |
BASIC_COUNT = 16; |
| 19 |
SPECIAL_HEX: array [0..139] of string = ( |
| 20 |
'000000', 'FAEBD7', '00FFFF', '7FFFD4', 'F0FFFF', 'F5F5DC', 'FFE4C4', |
| 21 |
'F0F8FF', 'FFEBCD', '0000FF', '8A2BE2', 'A52A2A', 'DEB887', '5F9EA0', |
| 22 |
'7FFF00', 'D2691E', 'FF7F50', '6495ED', 'FFF8DC', 'DC143C', '00FFFF', |
| 23 |
'00008B', '008B8B', 'B8860B', 'A9A9A9', '006400', 'BDB76B', '8B008B', |
| 24 |
'556B2F', 'FF8C00', '9932CC', '8B0000', 'E9967A', '8FBC8B', '483D8B', |
| 25 |
'2F4F4F', '00CED1', '9400D3', 'FF1493', '00BFFF', '696969', '1E90FF', |
| 26 |
'B22222', 'FFFAF0', '228B22', 'FF00FF', 'DCDCDC', 'F8F8FF', 'FFD700', |
| 27 |
'DAA520', '808080', '008000', 'ADFF2F', 'F0FFF0', 'FF69B4', 'CD5C5C', |
| 28 |
'4B0082', 'FFFFF0', 'F0E68C', 'E6E6FA', 'FFF0F5', '7CFC00', 'FFFACD', |
| 29 |
'ADD8E6', 'F08080', 'E0FFFF', 'FAFAD2', '90EE90', 'D3D3D3', 'FFB6C1', |
| 30 |
'FFA07A', '20B2AA', '87CEFA', '778899', 'B0C4DE', 'FFFFE0', '00FF00', |
| 31 |
'32CD32', 'FAF0E6', 'FF00FF', '800000', '66CDAA', '0000CD', 'BA55D3', |
| 32 |
'9370DB', '3CB371', '7B68EE', '00FA9A', '48D1CC', 'C71585', '191970', |
| 33 |
'F5FFFA', 'FFE4E1', 'FFE4B5', 'FFDEAD', '000080', 'FDF5E6', '808000', |
| 34 |
'6B8E23', 'FFA500', 'FF4500', 'DA70D6', 'EEE8AA', '98FB98', 'AFEEEE', |
| 35 |
'DB7093', 'FFEFD5', 'FFDAB9', 'CD853F', 'FFC0CB', 'DDA0DD', 'B0E0E6', |
| 36 |
'800080', 'FF0000', 'BC8F8F', '4169E1', '8B4513', 'FA8072', 'F4A460', |
| 37 |
'2E8B57', 'FFF5EE', 'A0522D', 'C0C0C0', '87CEEB', '6A5ACD', '708090', |
| 38 |
'FFFAFA', '00FF7F', '4682B4', 'D2B48C', '008080', 'D8BFD8', 'FF6347', |
| 39 |
'40E0D0', 'EE82EE', 'F5DEB3', 'FFFFFF', 'F5F5F5', 'FFFF00', '9ACD32' |
| 40 |
); |
| 41 |
SPECIAL_NAMES: array [0..139] of string = ( |
| 42 |
'black', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', |
| 43 |
'bisque', 'aliceblue', 'blanchedalmond', 'blue', 'blueviolet', 'brown', |
| 44 |
'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral', |
| 45 |
'cornflower', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', |
| 46 |
'darkgoldenrod', 'darkgray', 'darkgreen', 'darkkhaki', 'darkmagenta', |
| 47 |
'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', 'darksalmon', |
| 48 |
'darkseagreen', 'darkslateblue', 'darkslategray', 'darkturquoise', |
| 49 |
'darkviolet', 'deeppink', 'deepskyblue', 'dimgray', 'dodgerblue', |
| 50 |
'firebrick', 'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro', |
| 51 |
'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'greenyellow', |
| 52 |
'honeydew', 'hotpink', 'indianred', 'indigo', 'ivory', 'khaki', 'lavender', |
| 53 |
'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', |
| 54 |
'lightcyan', 'lightgoldenrodyellow', 'lightgreen', 'lightgray', 'lightpink', |
| 55 |
'lightsalmon', 'lightseagreen', 'lightskyblue', 'lightslategray', |
| 56 |
'lightsteelblue', 'lightyellow', 'lime', 'limegreen', 'linen', 'magenta', |
| 57 |
'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple', |
| 58 |
'mediumseagreen', 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', |
| 59 |
'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin', |
| 60 |
'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', |
| 61 |
'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', |
| 62 |
'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', |
| 63 |
'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown', |
| 64 |
'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue', |
| 65 |
'slategray', 'snow', 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', |
| 66 |
'tomato', 'turquoise', 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', |
| 67 |
'yellowgreen' |
| 68 |
); |
| 69 |
WEBSAFE_HEX: array [0..215] of string = ( |
| 70 |
'000000' ,'000033' ,'000066' ,'000099' ,'0000cc' ,'0000ff', |
| 71 |
'003300' ,'003333' ,'003366' ,'003399' ,'0033cc' ,'0033ff', |
| 72 |
'006600' ,'006633' ,'006666' ,'006699' ,'0066cc' ,'0066ff', |
| 73 |
'009900' ,'009933' ,'009966' ,'009999' ,'0099cc' ,'0099ff', |
| 74 |
'00cc00' ,'00cc33' ,'00cc66' ,'00cc99' ,'00cccc' ,'00ccff', |
| 75 |
'00ff00' ,'00ff33' ,'00ff66' ,'00ff99' ,'00ffcc' ,'00ffff', |
| 76 |
'330000' ,'330033' ,'330066' ,'330099' ,'3300cc' ,'3300ff', |
| 77 |
'333300' ,'333333' ,'333366' ,'333399' ,'3333cc' ,'3333ff', |
| 78 |
'336600' ,'336633' ,'336666' ,'336699' ,'3366cc' ,'3366ff', |
| 79 |
'339900' ,'339933' ,'339966' ,'339999' ,'3399cc' ,'3399ff', |
| 80 |
'33cc00' ,'33cc33' ,'33cc66' ,'33cc99' ,'33cccc' ,'33ccff', |
| 81 |
'33ff00' ,'33ff33' ,'33ff66' ,'33ff99' ,'33ffcc' ,'33ffff', |
| 82 |
'660000' ,'660033' ,'660066' ,'660099' ,'6600cc' ,'6600ff', |
| 83 |
'663300' ,'663333' ,'663366' ,'663399' ,'6633cc' ,'6633ff', |
| 84 |
'666600' ,'666633' ,'666666' ,'666699' ,'6666cc' ,'6666ff', |
| 85 |
'669900' ,'669933' ,'669966' ,'669999' ,'6699cc' ,'6699ff', |
| 86 |
'66cc00' ,'66cc33' ,'66cc66' ,'66cc99' ,'66cccc' ,'66ccff', |
| 87 |
'66ff00' ,'66ff33' ,'66ff66' ,'66ff99' ,'66ffcc' ,'66ffff', |
| 88 |
'990000' ,'990033' ,'990066' ,'990099' ,'9900cc' ,'9900ff', |
| 89 |
'993300' ,'993333' ,'993366' ,'993399' ,'9933cc' ,'9933ff', |
| 90 |
'996600' ,'996633' ,'996666' ,'996699' ,'9966cc' ,'9966ff', |
| 91 |
'999900' ,'999933' ,'999966' ,'999999' ,'9999cc' ,'9999ff', |
| 92 |
'99cc00' ,'99cc33' ,'99cc66' ,'99cc99' ,'99cccc' ,'99ccff', |
| 93 |
'99ff00' ,'99ff33' ,'99ff66' ,'99ff99' ,'99ffcc' ,'99ffff', |
| 94 |
'cc0000' ,'cc0033' ,'cc0066' ,'cc0099' ,'cc00cc' ,'cc00ff', |
| 95 |
'cc3300' ,'cc3333' ,'cc3366' ,'cc3399' ,'cc33cc' ,'cc33ff', |
| 96 |
'cc6600' ,'cc6633' ,'cc6666' ,'cc6699' ,'cc66cc' ,'cc66ff', |
| 97 |
'cc9900' ,'cc9933' ,'cc9966' ,'cc9999' ,'cc99cc' ,'cc99ff', |
| 98 |
'cccc00' ,'cccc33' ,'cccc66' ,'cccc99' ,'cccccc' ,'ccccff', |
| 99 |
'ccff00' ,'ccff33' ,'CCFF66' ,'ccff99' ,'ccffcc' ,'ccffff', |
| 100 |
'ff0000' ,'ff0033' ,'ff0066' ,'ff0099' ,'ff00cc' ,'ff00ff', |
| 101 |
'ff3300' ,'ff3333' ,'ff3366' ,'ff3399' ,'ff33cc' ,'ff33ff', |
| 102 |
'ff6600' ,'ff6633' ,'ff6666' ,'ff6699' ,'ff66cc' ,'ff66ff', |
| 103 |
'ff9900' ,'ff9933' ,'ff9966' ,'ff9999' ,'ff99cc' ,'ff99ff', |
| 104 |
'ffcc00' ,'ffcc33' ,'ffcc66' ,'ffcc99' ,'ffcccc' ,'ffccff', |
| 105 |
'ffff00' ,'ffff33' ,'ffff66' ,'ffff99' ,'ffffcc' ,'ffffff' |
| 106 |
); |
| 107 |
SYSTEM_VALUES: array [0..27] of TColor = ( |
| 108 |
clActiveBorder, clActiveCaption, clAppWorkspace, clBackground, |
| 109 |
clBtnFace, clBtnHighlight, clBtnShadow, clBtnText, clCaptionText, |
| 110 |
clGrayText, clHighlight, clHighlightText, clInactiveBorder, |
| 111 |
clInactiveCaption, clInactiveCaptionText, clInfoBk, clInfoText, |
| 112 |
clMenu, clMenuText, clScrollbar, cl3dDkShadow, cl3dLight, |
| 113 |
clBtnHighlight, clActiveBorder, clBtnShadow, clWindow, |
| 114 |
clWindowFrame, clWindowText |
| 115 |
); |
| 116 |
SYSTEM_NAMES: array [0..27] of string = ( |
| 117 |
'activeborder', 'activecaption', 'appworkspace', 'background', |
| 118 |
'buttonface', 'buttonhighlight', 'buttonshadow', 'buttontext', |
| 119 |
'captiontext', 'graytext', 'highlight', 'highlighttext', |
| 120 |
'inactiveborder', 'inactivecaption', 'inactivecaptiontext', |
| 121 |
'infobackground', 'infotext', 'menu', 'menutext', 'scrollbar', |
| 122 |
'threeddarkshadow', 'threedface', 'threedhighlight', |
| 123 |
'threedlightshadow', 'threedshadow', 'window', 'windowframe', |
| 124 |
'windowtext' |
| 125 |
); |
| 126 |
BASIC_VALUES: array [0..15] of TColor = ( |
| 127 |
clBlack, clAqua, clBlue, clFuchsia, clGray, clGreen, clLime, |
| 128 |
clMaroon, clNavy, clOlive, clPurple, clRed, clSilver, clTeal, |
| 129 |
clWhite, clYellow |
| 130 |
); |
| 131 |
BASIC_NAMES: array [0..15] of string = ( |
| 132 |
'black', 'aqua', 'blue', 'fuchsia', 'gray', 'green', 'lime', |
| 133 |
'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal', |
| 134 |
'white', 'yellow' |
| 135 |
); |
| 136 |
|
| 137 |
procedure MakeIntoHex(var s: string); |
| 138 |
function IsMember(a: array of string; n: integer; s: string): boolean; |
| 139 |
function IsSpecialColor(s: string): boolean; |
| 140 |
function FormatHexColor(S: string): string; |
| 141 |
function ColorToHex(Color: TColor): string; |
| 142 |
function HexToTColor(s: OleVariant): TColor; |
| 143 |
function GetHexFromName(s: string): string; |
| 144 |
function GetValueFromName(s: string): TColor; |
| 145 |
function IsWebSafe(s: string): boolean; overload; |
| 146 |
function IsWebSafe(c: TColor): boolean; overload; |
| 147 |
function GetWebSafe(C: TColor): TColor; |
| 148 |
|
| 149 |
implementation |
| 150 |
|
| 151 |
var |
| 152 |
WS: array [0..255] of byte; |
| 153 |
|
| 154 |
//checks membership of a string array |
| 155 |
function IsMember(a: array of string; n: integer; s: string): boolean; |
| 156 |
var |
| 157 |
i: integer; |
| 158 |
begin |
| 159 |
Result := false; |
| 160 |
for i := 0 to n - 1 do |
| 161 |
if SameText(s, a[i]) then |
| 162 |
Result := true; |
| 163 |
end; |
| 164 |
|
| 165 |
//checks if the color's name was used instead of hex |
| 166 |
function IsSpecialColor(s: string): boolean; |
| 167 |
begin |
| 168 |
Result := IsMember(BASIC_NAMES, BASIC_COUNT, s) or IsMember(SPECIAL_NAMES, SPECIAL_COUNT, s) or IsMember(SYSTEM_NAMES, SYSTEM_COUNT, s); |
| 169 |
end; |
| 170 |
|
| 171 |
//is hex was used then remove the wrong characters |
| 172 |
procedure MakeIntoHex(var s: string); |
| 173 |
var |
| 174 |
i: integer; |
| 175 |
begin |
| 176 |
if s <> '' then |
| 177 |
for i := 1 to Length(s) do |
| 178 |
if not (s[i] in ['0'..'9', 'A'..'F', 'a'..'f']) then |
| 179 |
s[i] := '0'; |
| 180 |
end; |
| 181 |
|
| 182 |
//formats entered text into a true hex value |
| 183 |
function FormatHexColor(S: string): string; |
| 184 |
var |
| 185 |
c: string; |
| 186 |
i: integer; |
| 187 |
begin |
| 188 |
c := ''; |
| 189 |
if not IsSpecialColor(s) then |
| 190 |
begin |
| 191 |
if (s <> '') and (s[1] = '#') then |
| 192 |
Delete(s, 1, 1); |
| 193 |
|
| 194 |
if s <> '' then |
| 195 |
begin |
| 196 |
MakeIntoHex(c); |
| 197 |
if Length(c) = 6 then |
| 198 |
Result := c |
| 199 |
else |
| 200 |
begin |
| 201 |
if Length(c) > 6 then |
| 202 |
c := Copy(c, 1, 6); |
| 203 |
if Length(c) < 6 then |
| 204 |
for i := 0 to 6 - Length(c) - 1 do |
| 205 |
c := '0' + c; |
| 206 |
Result := c; |
| 207 |
end; |
| 208 |
end |
| 209 |
else |
| 210 |
Result := '000000'; |
| 211 |
end |
| 212 |
else |
| 213 |
Result := s; |
| 214 |
end; |
| 215 |
|
| 216 |
//gets a hex value from a color name from special colors |
| 217 |
function GetHexFromName(s: string): string; |
| 218 |
var |
| 219 |
i, k: integer; |
| 220 |
begin |
| 221 |
k := 0; |
| 222 |
for i := 0 to SPECIAL_COUNT - 1 do |
| 223 |
if SameText(s, SPECIAL_NAMES[i]) then |
| 224 |
begin |
| 225 |
k := i; |
| 226 |
Break; |
| 227 |
end; |
| 228 |
Result := SPECIAL_HEX[k]; |
| 229 |
end; |
| 230 |
|
| 231 |
// gets a TColor value from a color name from basic or system colors |
| 232 |
function GetValueFromName(s: string): TColor; |
| 233 |
var |
| 234 |
i, k: integer; |
| 235 |
begin |
| 236 |
k := 0; |
| 237 |
s := LowerCase(s); |
| 238 |
if IsMember(BASIC_NAMES, BASIC_COUNT, s) then |
| 239 |
begin |
| 240 |
for i := 0 to BASIC_COUNT - 1 do |
| 241 |
if SameText(s, BASIC_NAMES[i]) then |
| 242 |
begin |
| 243 |
k := i; |
| 244 |
Break; |
| 245 |
end; |
| 246 |
Result := BASIC_VALUES[k]; |
| 247 |
end |
| 248 |
else if IsMember(SYSTEM_NAMES, SYSTEM_COUNT, s) then |
| 249 |
begin |
| 250 |
for i := 0 to SYSTEM_COUNT - 1 do |
| 251 |
if SameText(s, SYSTEM_NAMES[i]) then |
| 252 |
begin |
| 253 |
k := i; |
| 254 |
Break; |
| 255 |
end; |
| 256 |
Result := SYSTEM_VALUES[k]; |
| 257 |
end |
| 258 |
else |
| 259 |
Result := clNone; |
| 260 |
end; |
| 261 |
|
| 262 |
//converts a TColor value to a hex value |
| 263 |
function ColorToHex(Color: TColor): string; |
| 264 |
begin |
| 265 |
Result := IntToHex(GetRValue(Color), 2) + IntToHex(GetGValue(Color), 2) + IntToHex(GetBValue(Color), 2) |
| 266 |
end; |
| 267 |
|
| 268 |
//converts a hex value to a TColor |
| 269 |
function HexToTColor(s: OleVariant): TColor; |
| 270 |
begin |
| 271 |
if s <> null then |
| 272 |
begin |
| 273 |
if not IsSpecialColor(s) then |
| 274 |
begin |
| 275 |
s := FormatHexColor(s); |
| 276 |
if s <> '' then |
| 277 |
Result := RGB(StrToInt('$'+Copy(S, 1, 2)), StrToInt('$'+Copy(S, 3, 2)), StrToInt('$'+Copy(S, 5, 2))) |
| 278 |
else |
| 279 |
Result := clNone; |
| 280 |
end |
| 281 |
else if IsMember(SPECIAL_NAMES, SPECIAL_COUNT, s) then |
| 282 |
begin |
| 283 |
s := GetHexFromName(s); |
| 284 |
Result := RGB(StrToInt('$'+Copy(S, 1, 2)), StrToInt('$'+Copy(S, 3, 2)), StrToInt('$'+Copy(S, 5, 2))); |
| 285 |
end |
| 286 |
else |
| 287 |
Result := GetValueFromName(s); |
| 288 |
end |
| 289 |
else |
| 290 |
Result := clNone; |
| 291 |
end; |
| 292 |
|
| 293 |
//checks if a hex value belongs to the websafe palette |
| 294 |
function IsWebSafe(s: string): boolean; |
| 295 |
begin |
| 296 |
s := FormatHexColor(s); |
| 297 |
Result := IsMember(WEBSAFE_HEX, WEBSAFE_COUNT, s); |
| 298 |
end; |
| 299 |
|
| 300 |
//checks if a color belongs to the websafe palette |
| 301 |
function IsWebSafe(c: TColor): boolean; |
| 302 |
var |
| 303 |
s: string; |
| 304 |
begin |
| 305 |
s := ColorToHex(c); |
| 306 |
Result := IsMember(WEBSAFE_HEX, WEBSAFE_COUNT, s); |
| 307 |
end; |
| 308 |
|
| 309 |
//initializes the websafe comparison array |
| 310 |
procedure InitializeWS; |
| 311 |
var |
| 312 |
i: integer; |
| 313 |
begin |
| 314 |
for i := 0 to 255 do |
| 315 |
WS[I] := ((i + $19) div $33) * $33; |
| 316 |
end; |
| 317 |
|
| 318 |
//returns the closest web safe color to the one given |
| 319 |
function GetWebSafe(C: TColor): TColor; |
| 320 |
begin |
| 321 |
Result := RGB(WS[GetRValue(C)], WS[GetGValue(C)], WS[GetBValue(C)]); |
| 322 |
end; |
| 323 |
|
| 324 |
|
| 325 |
initialization |
| 326 |
InitializeWS; |
| 327 |
|
| 328 |
end. |