| 1 |
unit PalUtils; |
| 2 |
|
| 3 |
interface |
| 4 |
|
| 5 |
uses |
| 6 |
LCLType, LCLIntf, SysUtils, Classes, Graphics, |
| 7 |
//RGBHSVUtils, RGBHSLUtils, |
| 8 |
RGBCIEUtils, RGBCMYKUtils, |
| 9 |
HTMLColors; |
| 10 |
|
| 11 |
const |
| 12 |
clCustom = $2FFFFFFF; |
| 13 |
clTransparent = $3FFFFFFF; |
| 14 |
|
| 15 |
type |
| 16 |
TSortOrder = (soAscending, soDescending); |
| 17 |
TSortMode = (smRed, smGreen, smBlue, smHue, smSaturation, smLuminance, smValue, smNone, smCyan, smMagenta, smYellow, smBlacK, smCIEx, smCIEy, smCIEz, smCIEl, smCIEa, smCIEb); |
| 18 |
|
| 19 |
AcoColors = record |
| 20 |
Colors: array of TColor; |
| 21 |
Names: array of WideString; |
| 22 |
HasNames: boolean; |
| 23 |
end; |
| 24 |
|
| 25 |
//replaces passed strings with passed value |
| 26 |
function ReplaceFlags(s: string; flags: array of string; value: integer): string; |
| 27 |
|
| 28 |
//replaces the appropriate tags with values in a hint format string |
| 29 |
function FormatHint(fmt: string; c: TColor): string; |
| 30 |
|
| 31 |
//converts a string value to TColor including clCustom and clTransparent |
| 32 |
function mbStringToColor(s: string): TColor; |
| 33 |
|
| 34 |
//converts a TColor to a string value including clCustom and clTransparent |
| 35 |
function mbColorToString(c: TColor): string; |
| 36 |
|
| 37 |
//blends two colors together in proportion C1 : C2 = W1 : 100 - W1, where 0 <= W1 <= 100 |
| 38 |
function Blend(C1, C2: TColor; W1: Integer): TColor; |
| 39 |
|
| 40 |
// Inverts a color |
| 41 |
function InvertedColor(C: TColor): TColor; |
| 42 |
|
| 43 |
//generates a white-color-black or a black-color-white gradient palette |
| 44 |
function MakePalette(BaseColor: TColor; SortOrder: TSortOrder): string; |
| 45 |
|
| 46 |
//generates a gradient palette from the given colors |
| 47 |
function MakeGradientPalette(Colors: array of TColor): string; |
| 48 |
|
| 49 |
//sorts colors in a string list |
| 50 |
procedure SortPalColors(Colors: TStrings; SortMode: TSortMode; SortOrder: TSortOrder); |
| 51 |
|
| 52 |
//reads JASC .pal file |
| 53 |
function ReadJASCPal(PalFile: TFileName): string; |
| 54 |
|
| 55 |
//saves a string list to a JASC .pal file |
| 56 |
procedure SaveJASCPal(pal: TStrings; FileName: TFileName); |
| 57 |
|
| 58 |
//reads Photoshop .aco file into an Aco record |
| 59 |
function ReadPhotoshopAco(PalFile: TFileName): AcoColors; |
| 60 |
|
| 61 |
//reads Photoshop .act file |
| 62 |
function ReadPhotoshopAct(PalFile: TFileName): string; |
| 63 |
|
| 64 |
|
| 65 |
implementation |
| 66 |
|
| 67 |
uses |
| 68 |
Math, mbColorConv; |
| 69 |
|
| 70 |
function ReplaceFlags(s: string; flags: array of string; value: integer): string; |
| 71 |
var |
| 72 |
i, p: integer; |
| 73 |
v: string; |
| 74 |
begin |
| 75 |
Result := s; |
| 76 |
v := IntToStr(value); |
| 77 |
for i := 0 to Length(flags) - 1 do |
| 78 |
begin |
| 79 |
p := Pos(flags[i], Result); |
| 80 |
if p > 0 then |
| 81 |
begin |
| 82 |
Delete(Result, p, Length(flags[i])); |
| 83 |
Insert(v, Result, p); |
| 84 |
end; |
| 85 |
end; |
| 86 |
end; |
| 87 |
|
| 88 |
function AnsiReplaceText(const AText, AFromText, AToText: string): string; |
| 89 |
begin |
| 90 |
Result := StringReplace(AText, AFromText, AToText, [rfReplaceAll, rfIgnoreCase]); |
| 91 |
end; |
| 92 |
|
| 93 |
function FormatHint(fmt: string; c: TColor): string; |
| 94 |
var |
| 95 |
h: string; |
| 96 |
hslH, hslS, hslL: Double; |
| 97 |
hsvH, hsvS, hsvV: Double; |
| 98 |
begin |
| 99 |
ColorToHSL(c, hslH, hslS, hslL); |
| 100 |
ColorToHSV(c, hsvH, hsvS, hsvV); |
| 101 |
|
| 102 |
h := AnsiReplaceText(fmt, '%hex', '#' + ColorToHex(c)); |
| 103 |
h := AnsiReplaceText(h, '%cieL', IntToStr(Round(GetCIElValue(c)))); |
| 104 |
h := AnsiReplaceText(h, '%cieA', IntToStr(Round(GetCIEaValue(c)))); |
| 105 |
h := AnsiReplaceText(h, '%cieB', IntToStr(Round(GetCIEbValue(c)))); |
| 106 |
h := AnsiReplaceText(h, '%cieX', IntToStr(Round(GetCIExValue(c)))); |
| 107 |
h := AnsiReplaceText(h, '%cieY', IntToStr(Round(GetCIEyValue(c)))); |
| 108 |
h := AnsiReplaceText(h, '%cieZ', IntToStr(Round(GetCIEzValue(c)))); |
| 109 |
h := AnsiReplaceText(h, '%cieC', IntToStr(Round(GetCIEcValue(c)))); |
| 110 |
h := AnsiReplaceText(h, '%cieH', IntToStr(Round(GetCIEhValue(c)))); |
| 111 |
h := AnsiReplaceText(h, '%hslH', IntToStr(Round(hslH * 360))); //RGBHSLUtils.GetHValue(c))); |
| 112 |
h := AnsiReplaceText(h, '%hslS', IntToStr(Round(hslS * 255))); //RGBHSLUtils.GetSValue(c))); |
| 113 |
h := AnsiReplaceText(h, '%hslL', IntToStr(Round(hslL * 255))); //RGBHSLUtils.GetLValue(c))); |
| 114 |
h := AnsiReplaceText(h, '%hsvH', IntToStr(round(hsvH * 360))); //RGBHSVUtils.GetHValue(c))); |
| 115 |
h := AnsiReplaceText(h, '%hsvS', IntToStr(round(hsvS * 255))); //RGBHSVUtils.GetSValue(c))); |
| 116 |
h := AnsiReplaceText(h, '%hsvV', IntToStr(round(hsvV * 255))); //RGBHSVUtils.GetVValue(c))); |
| 117 |
h := AnsiReplaceText(h, '%r', IntToStr(GetRValue(c))); |
| 118 |
h := AnsiReplaceText(h, '%g', IntToStr(GetGValue(c))); |
| 119 |
h := AnsiReplaceText(h, '%b', IntToStr(GetBValue(c))); |
| 120 |
h := AnsiReplaceText(h, '%c', IntToStr(GetCValue(c))); |
| 121 |
h := AnsiReplaceText(h, '%m', IntToStr(GetMValue(c))); |
| 122 |
h := AnsiReplaceText(h, '%y', IntToStr(GetYValue(c))); |
| 123 |
h := AnsiReplaceText(h, '%k', IntToStr(GetKValue(c))); |
| 124 |
h := AnsiReplaceText(h, '%h', IntToStr(round(hslH * 360))); //RGBHSLUtils.GetHValue(c))); |
| 125 |
h := AnsiReplaceText(h, '%s', IntToStr(round(hslS * 255))); //RGBHSLUtils.GetSValue(c))); |
| 126 |
h := AnsiReplaceText(h, '%l', IntToStr(round(hslL * 255))); //RGBHSLUtils.GetLValue(c))); |
| 127 |
h := AnsiReplaceText(h, '%v', IntToStr(round(hsvV * 255))); //RGBHSVUtils.GetVValue(c))); |
| 128 |
Result := h; |
| 129 |
end; |
| 130 |
|
| 131 |
function mbStringToColor(s: string): TColor; |
| 132 |
begin |
| 133 |
//remove spaces |
| 134 |
s := AnsiReplaceText(s, ' ', ''); |
| 135 |
if SameText(s, 'clCustom') then |
| 136 |
Result := clCustom |
| 137 |
else |
| 138 |
if SameText(s, 'clTransparent') then |
| 139 |
Result := clTransparent |
| 140 |
else |
| 141 |
Result := StringToColor(s); |
| 142 |
end; |
| 143 |
|
| 144 |
function mbColorToString(c: TColor): string; |
| 145 |
begin |
| 146 |
if c = clCustom then |
| 147 |
Result := 'clCustom' |
| 148 |
else |
| 149 |
if c = clTransparent then |
| 150 |
Result := 'clTransparent' |
| 151 |
else |
| 152 |
Result := ColorToString(c); |
| 153 |
end; |
| 154 |
|
| 155 |
//taken from TBXUtils, TBX Package � Alex Denisov (www.g32.org) |
| 156 |
function Blend(C1, C2: TColor; W1: Integer): TColor; |
| 157 |
var |
| 158 |
W2, A1, A2, D, F, G: Integer; |
| 159 |
begin |
| 160 |
if C1 < 0 then C1 := GetSysColor(C1 and $FF); |
| 161 |
if C2 < 0 then C2 := GetSysColor(C2 and $FF); |
| 162 |
|
| 163 |
if W1 >= 100 then D := 1000 |
| 164 |
else D := 100; |
| 165 |
|
| 166 |
W2 := D - W1; |
| 167 |
F := D div 2; |
| 168 |
|
| 169 |
A2 := C2 shr 16 * W2; |
| 170 |
A1 := C1 shr 16 * W1; |
| 171 |
G := (A1 + A2 + F) div D and $FF; |
| 172 |
Result := G shl 16; |
| 173 |
|
| 174 |
A2 := (C2 shr 8 and $FF) * W2; |
| 175 |
A1 := (C1 shr 8 and $FF) * W1; |
| 176 |
G := (A1 + A2 + F) div D and $FF; |
| 177 |
Result := Result or G shl 8; |
| 178 |
|
| 179 |
A2 := (C2 and $FF) * W2; |
| 180 |
A1 := (C1 and $FF) * W1; |
| 181 |
G := (A1 + A2 + F) div D and $FF; |
| 182 |
Result := Result or G; |
| 183 |
end; |
| 184 |
|
| 185 |
function InvertedColor(C: TColor): TColor; |
| 186 |
begin |
| 187 |
Result := RgbToColor(255 - GetRValue(c), 255 - GetGValue(c), 255 - GetBValue(c)); |
| 188 |
end; |
| 189 |
|
| 190 |
function IsMember(sl: TStrings; s: string): boolean; |
| 191 |
var |
| 192 |
i: integer; |
| 193 |
begin |
| 194 |
for i := 0 to sl.count -1 do |
| 195 |
if sl.Strings[i] = s then |
| 196 |
begin |
| 197 |
Result := true; |
| 198 |
exit; |
| 199 |
end; |
| 200 |
Result := false; |
| 201 |
end; |
| 202 |
|
| 203 |
function MakePalette(BaseColor: TColor; SortOrder: TSortOrder): string; |
| 204 |
const |
| 205 |
maxL = 240; |
| 206 |
var |
| 207 |
i: integer; |
| 208 |
s: TStrings; |
| 209 |
hslH, hslS, hslL: Double; |
| 210 |
begin |
| 211 |
Result := ''; |
| 212 |
s := TStringList.Create; |
| 213 |
try |
| 214 |
ColorToHSL(BaseColor, hslH, hslS, hslL); |
| 215 |
case SortOrder of |
| 216 |
soAscending: |
| 217 |
for i := maxL downto 0 do |
| 218 |
s.Add(ColorToString(HSLToColor(hslH, hslS, 1 - i/maxL))); |
| 219 |
// for i := 239 downto 0 do |
| 220 |
// s.Add(ColorToString(HSLRangeToRGB(GetHValue(BaseColor), GetSValue(BaseColor), 240 - i))); |
| 221 |
soDescending: |
| 222 |
for i := 0 to maxL do |
| 223 |
s.Add(ColorToString(HSLToColor(hslH, hslS, i/maxL))); |
| 224 |
// for i := 0 to 239 do |
| 225 |
// s.Add(ColorToString(HSLRangeToRGB(GetHValue(BaseColor), GetSValue(BaseColor), 240 - i))); |
| 226 |
end; |
| 227 |
Result := s.Text; |
| 228 |
finally |
| 229 |
s.Free; |
| 230 |
end; |
| 231 |
end; |
| 232 |
|
| 233 |
function MakeGradientPalette(Colors: array of TColor): string; |
| 234 |
type |
| 235 |
RGBArray = array[0..2] of Byte; |
| 236 |
var |
| 237 |
i, j, k, Span: Integer; |
| 238 |
s: TStringList; |
| 239 |
Scolor: string; |
| 240 |
Faktor: double; |
| 241 |
a: RGBArray; |
| 242 |
b: array of RGBArray; |
| 243 |
begin |
| 244 |
Result := ''; |
| 245 |
Span := 300; |
| 246 |
s := TStringList.Create; |
| 247 |
try |
| 248 |
SetLength(b, High(Colors) + 1); |
| 249 |
for i := 0 to High(Colors) do |
| 250 |
begin |
| 251 |
Colors[i] := ColorToRGB(Colors[i]); |
| 252 |
b[i, 0] := GetRValue(Colors[i]); |
| 253 |
b[i, 1] := GetGValue(Colors[i]); |
| 254 |
b[i, 2] := GetBValue(Colors[i]); |
| 255 |
end; |
| 256 |
for i := 0 to High(Colors) - 1 do |
| 257 |
for j := 0 to Span do |
| 258 |
begin |
| 259 |
Faktor := j / Span; |
| 260 |
for k := 0 to 3 do |
| 261 |
a[k] := Trunc(b[i, k] + ((b[i + 1, k] - b[i, k]) * Faktor)); |
| 262 |
Scolor := ColorToString(RGB(a[0], a[1], a[2])); |
| 263 |
if not IsMember(s, Scolor) then |
| 264 |
s.add(Scolor); |
| 265 |
end; |
| 266 |
Result := s.Text; |
| 267 |
finally |
| 268 |
s.Free; |
| 269 |
end; |
| 270 |
end; |
| 271 |
|
| 272 |
procedure SortPalColors(Colors: TStrings; SortMode: TSortMode; SortOrder: TSortOrder); |
| 273 |
|
| 274 |
function MaxPos(s: TStrings; sm: TSortMode): integer; |
| 275 |
var |
| 276 |
i: integer; |
| 277 |
first: TColor; |
| 278 |
c: TColor; |
| 279 |
hc, sc, lc, vc: Double; |
| 280 |
hf, sf, lf, vf: Double; |
| 281 |
begin |
| 282 |
Result := 0; |
| 283 |
first := clBlack; |
| 284 |
for i := 0 to s.Count - 1 do |
| 285 |
begin |
| 286 |
c := mbStringToColor(s.Strings[i]); |
| 287 |
case sm of |
| 288 |
smRed: |
| 289 |
if GetRValue(first) < GetRValue(c) then |
| 290 |
begin |
| 291 |
first := c; |
| 292 |
Result := i; |
| 293 |
end; |
| 294 |
smGreen: |
| 295 |
if GetGValue(first) < GetGValue(c) then |
| 296 |
begin |
| 297 |
first := c; |
| 298 |
Result := i; |
| 299 |
end; |
| 300 |
smBlue: |
| 301 |
if GetBValue(first) < GetBValue(c) then |
| 302 |
begin |
| 303 |
first := c; |
| 304 |
Result := i; |
| 305 |
end; |
| 306 |
smHue: |
| 307 |
begin |
| 308 |
ColorToHSL(c, hc, sc, lc); |
| 309 |
ColorToHSL(first, hf, sf, lf); |
| 310 |
if hf < hc then begin |
| 311 |
first := c; |
| 312 |
Result := i; |
| 313 |
end; |
| 314 |
end; |
| 315 |
smSaturation: |
| 316 |
begin |
| 317 |
ColorToHSL(c, hc, sc, lc); |
| 318 |
ColorToHSL(first, hf, sf, lf); |
| 319 |
if sf < sc then begin |
| 320 |
first := c; |
| 321 |
Result := i; |
| 322 |
end; |
| 323 |
end; |
| 324 |
smLuminance: |
| 325 |
begin |
| 326 |
ColorToHSL(c, hc, sc, lc); |
| 327 |
ColorToHSL(first, hf, sc, lf); |
| 328 |
if lf < lc then |
| 329 |
begin |
| 330 |
first := c; |
| 331 |
Result := i; |
| 332 |
end; |
| 333 |
end; |
| 334 |
smValue: |
| 335 |
begin |
| 336 |
ColorToHSV(c, hc, sc, vc); |
| 337 |
ColorToHSV(first, hf, sc, vf); |
| 338 |
if vf < vc then |
| 339 |
begin |
| 340 |
first := c; |
| 341 |
Result := i; |
| 342 |
end; |
| 343 |
end; |
| 344 |
smCyan: |
| 345 |
if GetCValue(first) < GetCValue(c) then |
| 346 |
begin |
| 347 |
first := c; |
| 348 |
Result := i; |
| 349 |
end; |
| 350 |
smMagenta: |
| 351 |
if GetMValue(first) < GetMValue(c) then |
| 352 |
begin |
| 353 |
first := c; |
| 354 |
Result := i; |
| 355 |
end; |
| 356 |
smYellow: |
| 357 |
if GetYValue(first) < GetYValue(c) then |
| 358 |
begin |
| 359 |
first := c; |
| 360 |
Result := i; |
| 361 |
end; |
| 362 |
smBlacK: |
| 363 |
if GetKValue(first) < GetKValue(c) then |
| 364 |
begin |
| 365 |
first := c; |
| 366 |
Result := i; |
| 367 |
end; |
| 368 |
smCIEx: |
| 369 |
if GetCIEXValue(first) < GetCIEXValue(c) then |
| 370 |
begin |
| 371 |
first := c; |
| 372 |
Result := i; |
| 373 |
end; |
| 374 |
smCIEy: |
| 375 |
if GetCIEYValue(first) < GetCIEYValue(c) then |
| 376 |
begin |
| 377 |
first := c; |
| 378 |
Result := i; |
| 379 |
end; |
| 380 |
smCIEz: |
| 381 |
if GetCIEZValue(first) < GetCIEZValue(c) then |
| 382 |
begin |
| 383 |
first := c; |
| 384 |
Result := i; |
| 385 |
end; |
| 386 |
smCIEl: |
| 387 |
if GetCIELValue(first) < GetCIELValue(c) then |
| 388 |
begin |
| 389 |
first := c; |
| 390 |
Result := i; |
| 391 |
end; |
| 392 |
smCIEa: |
| 393 |
if GetCIEAValue(first) < GetCIEAValue(c) then |
| 394 |
begin |
| 395 |
first := c; |
| 396 |
Result := i; |
| 397 |
end; |
| 398 |
smCIEb: |
| 399 |
if GetCIEBValue(first) < GetCIEBValue(c) then |
| 400 |
begin |
| 401 |
first := c; |
| 402 |
Result := i; |
| 403 |
end; |
| 404 |
end; |
| 405 |
end; |
| 406 |
end; |
| 407 |
|
| 408 |
function MinPos(s: TStrings; sm: TSortMode): integer; |
| 409 |
var |
| 410 |
i: integer; |
| 411 |
first: TColor; |
| 412 |
c: TColor; |
| 413 |
hc, sc, lc, vc: Double; |
| 414 |
hf, sf, lf, vf: Double; |
| 415 |
begin |
| 416 |
Result := 0; |
| 417 |
first := clWhite; |
| 418 |
for i := 0 to s.Count - 1 do |
| 419 |
begin |
| 420 |
c := mbStringToColor(s.Strings[i]); |
| 421 |
case sm of |
| 422 |
smRed: |
| 423 |
if GetRValue(first) > GetRValue(c) then |
| 424 |
begin |
| 425 |
first := c; |
| 426 |
Result := i; |
| 427 |
end; |
| 428 |
smGreen: |
| 429 |
if GetGValue(first) > GetGValue(c) then |
| 430 |
begin |
| 431 |
first := c; |
| 432 |
Result := i; |
| 433 |
end; |
| 434 |
smBlue: |
| 435 |
if GetBValue(first) > GetBValue(c) then |
| 436 |
begin |
| 437 |
first := c; |
| 438 |
Result := i; |
| 439 |
end; |
| 440 |
smHue: |
| 441 |
begin |
| 442 |
ColorToHSL(c, hc, sc, lc); |
| 443 |
ColorToHSL(first, hf, sf, lf); |
| 444 |
if hf > hc then |
| 445 |
begin |
| 446 |
first := c; |
| 447 |
Result := i; |
| 448 |
end; |
| 449 |
end; |
| 450 |
smSaturation: |
| 451 |
begin |
| 452 |
ColorToHSL(c, hc, sc, lc); |
| 453 |
ColorToHSV(first, hf, sf, vf); |
| 454 |
if sf > sc then |
| 455 |
begin |
| 456 |
first := c; |
| 457 |
Result := i; |
| 458 |
end; |
| 459 |
end; |
| 460 |
smLuminance: |
| 461 |
begin |
| 462 |
ColorToHSL(c, hc, sc, lc); |
| 463 |
ColorToHSV(first, hf, sf, vf); |
| 464 |
if lf > lc then |
| 465 |
begin |
| 466 |
first := c; |
| 467 |
Result := i; |
| 468 |
end; |
| 469 |
end; |
| 470 |
smValue: |
| 471 |
begin |
| 472 |
ColorToHSV(c, hc, sc, vc); |
| 473 |
ColorToHSV(first, hf, sf, vf); |
| 474 |
if vf > vc then |
| 475 |
begin |
| 476 |
first := c; |
| 477 |
Result := i; |
| 478 |
end; |
| 479 |
end; |
| 480 |
smCyan: |
| 481 |
if GetCValue(first) > GetCValue(c) then |
| 482 |
begin |
| 483 |
first := c; |
| 484 |
Result := i; |
| 485 |
end; |
| 486 |
smMagenta: |
| 487 |
if GetMValue(first) > GetMValue(c) then |
| 488 |
begin |
| 489 |
first := c; |
| 490 |
Result := i; |
| 491 |
end; |
| 492 |
smYellow: |
| 493 |
if GetYValue(first) > GetYValue(c) then |
| 494 |
begin |
| 495 |
first := c; |
| 496 |
Result := i; |
| 497 |
end; |
| 498 |
smBlacK: |
| 499 |
if GetKValue(first) > GetKValue(c) then |
| 500 |
begin |
| 501 |
first := c; |
| 502 |
Result := i; |
| 503 |
end; |
| 504 |
smCIEx: |
| 505 |
if GetCIEXValue(first) > GetCIEXValue(c) then |
| 506 |
begin |
| 507 |
first := c; |
| 508 |
Result := i; |
| 509 |
end; |
| 510 |
smCIEy: |
| 511 |
if GetCIEYValue(first) > GetCIEYValue(c) then |
| 512 |
begin |
| 513 |
first := c; |
| 514 |
Result := i; |
| 515 |
end; |
| 516 |
smCIEz: |
| 517 |
if GetCIEZValue(first) > GetCIEZValue(c) then |
| 518 |
begin |
| 519 |
first := c; |
| 520 |
Result := i; |
| 521 |
end; |
| 522 |
smCIEl: |
| 523 |
if GetCIELValue(first) > GetCIELValue(c) then |
| 524 |
begin |
| 525 |
first := c; |
| 526 |
Result := i; |
| 527 |
end; |
| 528 |
smCIEa: |
| 529 |
if GetCIEAValue(first) > GetCIEAValue(c) then |
| 530 |
begin |
| 531 |
first := c; |
| 532 |
Result := i; |
| 533 |
end; |
| 534 |
smCIEb: |
| 535 |
if GetCIEBValue(first) > GetCIEBValue(c) then |
| 536 |
begin |
| 537 |
first := c; |
| 538 |
Result := i; |
| 539 |
end; |
| 540 |
end; |
| 541 |
end; |
| 542 |
end; |
| 543 |
|
| 544 |
var |
| 545 |
i, m: integer; |
| 546 |
s: TStrings; |
| 547 |
begin |
| 548 |
if SortMode <> smNone then |
| 549 |
begin |
| 550 |
if Colors.Count = 0 then Exit; |
| 551 |
m := 0; |
| 552 |
s := TStringList.Create; |
| 553 |
try |
| 554 |
s.AddStrings(Colors); |
| 555 |
Colors.Clear; |
| 556 |
for i := s.Count - 1 downto 0 do |
| 557 |
begin |
| 558 |
case SortOrder of |
| 559 |
soAscending : m := MinPos(s, SortMode); |
| 560 |
soDescending : m := MaxPos(s, SortMode); |
| 561 |
end; |
| 562 |
Colors.Add(s.Strings[m]); |
| 563 |
s.Delete(m); |
| 564 |
end; |
| 565 |
finally |
| 566 |
s.Free; |
| 567 |
end; |
| 568 |
end; |
| 569 |
end; |
| 570 |
|
| 571 |
function ReadJASCPal(PalFile: TFileName): string; |
| 572 |
var |
| 573 |
p, t, c: TStrings; |
| 574 |
i: integer; |
| 575 |
begin |
| 576 |
if not FileExists(PalFile) then |
| 577 |
raise Exception.Create('File not found'); |
| 578 |
|
| 579 |
p := TStringList.Create; |
| 580 |
t := TStringList.Create; |
| 581 |
c := TStringList.Create; |
| 582 |
try |
| 583 |
p.LoadFromFile(PalFile); |
| 584 |
for i := 0 to p.Count - 1 do |
| 585 |
if p.strings[i] <> '' then |
| 586 |
begin |
| 587 |
t.Clear; |
| 588 |
ExtractStrings([' '], [], PChar(p.strings[i]), t); |
| 589 |
if t.Count = 3 then |
| 590 |
c.Add(ColorToString(RGB(StrToInt(t.strings[0]), StrToInt(t.strings[1]), StrToInt(t.strings[2])))); |
| 591 |
end; |
| 592 |
Result := c.Text; |
| 593 |
finally |
| 594 |
c.Free; |
| 595 |
t.Free; |
| 596 |
p.Free; |
| 597 |
end; |
| 598 |
end; |
| 599 |
|
| 600 |
procedure SaveJASCPal(pal: TStrings; FileName: TFileName); |
| 601 |
var |
| 602 |
i: integer; |
| 603 |
p: TStringList; |
| 604 |
c: TColor; |
| 605 |
begin |
| 606 |
if not FileExists(FileName) then |
| 607 |
raise Exception.Create('File not found'); |
| 608 |
|
| 609 |
p := TStringList.Create; |
| 610 |
try |
| 611 |
p.Add('JASC-PAL'); |
| 612 |
p.Add('0100'); |
| 613 |
p.Add('256'); |
| 614 |
for i := 0 to pal.Count - 1 do |
| 615 |
if (pal.Strings[i] <> '') and not SameText(pal.Strings[i], 'clCustom') and not SameText(pal.Strings[i], 'clTransparent') then |
| 616 |
begin |
| 617 |
c := StringToColor(pal.Strings[i]); |
| 618 |
p.Add(IntToStr(GetRValue(c)) + ' ' + IntToStr(GetGValue(c)) + ' ' + IntToStr(GetBValue(c))); |
| 619 |
end; |
| 620 |
p.SaveToFile(FileName); |
| 621 |
finally |
| 622 |
p.Free; |
| 623 |
end; |
| 624 |
end; |
| 625 |
|
| 626 |
procedure ExchangeBytes(var w: Word); |
| 627 |
type |
| 628 |
TWordRec = packed record |
| 629 |
a, b: byte; |
| 630 |
end; |
| 631 |
var |
| 632 |
brec: TWordRec; |
| 633 |
tmp: Byte; |
| 634 |
begin |
| 635 |
brec := TWordRec(w); |
| 636 |
tmp := brec.a; |
| 637 |
brec.a := brec.b; |
| 638 |
brec.b := tmp; |
| 639 |
w := word(brec); |
| 640 |
|
| 641 |
// Swap(w); |
| 642 |
{ |
| 643 |
asm |
| 644 |
MOV DX,[w] //assign the word to the data register |
| 645 |
XCHG DL,DH // exchange low and high data values |
| 646 |
MOV [w],DX //assign the register data to the word |
| 647 |
} |
| 648 |
end; |
| 649 |
|
| 650 |
procedure ExchangeChars(var s: WideString); |
| 651 |
var |
| 652 |
i: Integer; |
| 653 |
w: Word; |
| 654 |
begin |
| 655 |
for i := 1 to Length(s) do |
| 656 |
begin |
| 657 |
w := Word(s[i]); |
| 658 |
ExchangeBytes(w); |
| 659 |
s[i] := WideChar(w); |
| 660 |
end; |
| 661 |
end; |
| 662 |
|
| 663 |
function GetAcoColor(space,w,x,y,z: word): TColor; |
| 664 |
begin |
| 665 |
case space of |
| 666 |
0: //RGB |
| 667 |
Result := RGB(w div 256, x div 256, y div 256); |
| 668 |
1: //HSB - HSV |
| 669 |
Result := HSVToColor(Round(w/182.04), Round(x/655.35), Round(y/655.35)); |
| 670 |
2: //CMYK |
| 671 |
Result := CMYKToColor(Round(100-w/55.35), Round(100-x/655.35), Round(100-y/655.35), Round(100-z/655.35)); |
| 672 |
7: //Lab |
| 673 |
Result := LabToRGB(w/100, x/100, y/100); |
| 674 |
8: //Grayscale |
| 675 |
Result := RGB(Round(w/39.0625), Round(w/39.0625), Round(w/39.0625)); |
| 676 |
9: //Wide CMYK |
| 677 |
Result := CMYKToColor(w div 100, x div 100, y div 100, z div 100) |
| 678 |
else //unknown |
| 679 |
Result := RGB(w div 256, x div 256, y div 256); |
| 680 |
end; |
| 681 |
end; |
| 682 |
|
| 683 |
function ReadPhotoshopAco(PalFile: TFileName): AcoColors; |
| 684 |
var |
| 685 |
f: file; |
| 686 |
ver, num, space, w, x, y, z, dummy: Word; |
| 687 |
i: integer; |
| 688 |
v0Length: byte; |
| 689 |
v0Name: string; |
| 690 |
v2Length: Word; |
| 691 |
v2Name: WideString; |
| 692 |
begin |
| 693 |
Initialize(Result); |
| 694 |
|
| 695 |
if not FileExists(PalFile) then |
| 696 |
begin |
| 697 |
SetLength(Result.Colors, 0); |
| 698 |
SetLength(Result.Names, 0); |
| 699 |
Result.HasNames := false; |
| 700 |
raise Exception.Create('File not found'); |
| 701 |
end; |
| 702 |
|
| 703 |
AssignFile(f, PalFile); |
| 704 |
Reset(f, 1); |
| 705 |
//read version |
| 706 |
BlockRead(f, ver, sizeof(ver)); |
| 707 |
ExchangeBytes(ver); |
| 708 |
if not (ver in [0, 1, 2]) then |
| 709 |
begin |
| 710 |
CloseFile(f); |
| 711 |
raise Exception.Create('The file you are trying to load is not (yet) supported.'#13'Please submit the file for testing to MXS so loading of this version will be supported too'); |
| 712 |
end; |
| 713 |
|
| 714 |
//read number of colors |
| 715 |
BlockRead(f, num, sizeof(num)); |
| 716 |
ExchangeBytes(num); |
| 717 |
//read names |
| 718 |
if (ver = 0) or (ver = 2) then |
| 719 |
begin |
| 720 |
SetLength(Result.Names, num); |
| 721 |
Result.HasNames := true; |
| 722 |
end |
| 723 |
else |
| 724 |
begin |
| 725 |
SetLength(Result.Names, 0); |
| 726 |
Result.HasNames := false; |
| 727 |
end; |
| 728 |
//read colors |
| 729 |
SetLength(Result.Colors, num); |
| 730 |
for i := 0 to num - 1 do |
| 731 |
begin |
| 732 |
BlockRead(f, space, sizeof(space)); |
| 733 |
ExchangeBytes(space); |
| 734 |
BlockRead(f, w, sizeof(w)); |
| 735 |
ExchangeBytes(w); |
| 736 |
BlockRead(f, x, sizeof(x)); |
| 737 |
ExchangeBytes(x); |
| 738 |
BlockRead(f, y, sizeof(y)); |
| 739 |
ExchangeBytes(y); |
| 740 |
BlockRead(f, z, sizeof(z)); |
| 741 |
ExchangeBytes(z); |
| 742 |
Result.Colors[i] := GetAcoColor(space, w, x, y, z); |
| 743 |
case ver of |
| 744 |
0: begin |
| 745 |
BlockRead(f, v0Length, SizeOf(v0Length)); |
| 746 |
SetLength(v0Name, v0Length); |
| 747 |
if v0Length > 0 then |
| 748 |
BlockRead(f, PChar(v0Name)^, v0Length); |
| 749 |
Result.Names[i] := WideString(v0Name); |
| 750 |
end; |
| 751 |
2: begin |
| 752 |
BlockRead(f, dummy, sizeof(dummy)); |
| 753 |
BlockRead(f, v2Length, SizeOf(v2Length)); |
| 754 |
ExchangeBytes(v2Length); |
| 755 |
SetLength(v2Name, v2Length - 1); |
| 756 |
if v2Length > 0 then |
| 757 |
begin |
| 758 |
BlockRead(f, PWideChar(v2Name)^, 2*(v2Length - 1)); |
| 759 |
ExchangeChars(v2Name); |
| 760 |
end; |
| 761 |
Result.Names[i] := v2Name; |
| 762 |
BlockRead(f, dummy, sizeof(dummy)); |
| 763 |
end; |
| 764 |
end; |
| 765 |
end; |
| 766 |
CloseFile(f); |
| 767 |
end; |
| 768 |
|
| 769 |
function ReadPhotoshopAct(PalFile: TFileName): string; |
| 770 |
var |
| 771 |
f: file; |
| 772 |
r, g, b: byte; |
| 773 |
s: TStringList; |
| 774 |
i: integer; |
| 775 |
begin |
| 776 |
if not FileExists(PalFile) then |
| 777 |
raise Exception.Create('File not found'); |
| 778 |
|
| 779 |
s := TStringList.Create; |
| 780 |
try |
| 781 |
AssignFile(f, PalFile); |
| 782 |
Reset(f, 1); |
| 783 |
for i := 0 to 255 do |
| 784 |
begin |
| 785 |
BlockRead(f, r, sizeof(r)); |
| 786 |
BlockRead(f, g, sizeof(g)); |
| 787 |
BlockRead(f, b, sizeof(b)); |
| 788 |
s.Add(ColorToString(RGB(r, g, b))); |
| 789 |
end; |
| 790 |
Result := s.Text; |
| 791 |
finally |
| 792 |
s.Free; |
| 793 |
end; |
| 794 |
CloseFile(f); |
| 795 |
end; |
| 796 |
|
| 797 |
end. |