cocoa: implement GetSysColor for pattern colors like clWindow and clBtnFace

git-svn-id: trunk@34391 -
This commit is contained in:
paul 2011-12-24 16:23:01 +00:00
parent 02a0fa9668
commit f1976687d0
2 changed files with 53 additions and 3 deletions

View File

@ -1034,6 +1034,8 @@ end;
procedure TCocoaContext.SetBkColor(AValue: TColor);
begin
if AValue = clBtnFace then
AValue := AValue;
AValue := TColor(ColorToRGB(AValue));
FBkColor := AValue;
FBkBrush.SetColor(AValue, BkMode = OPAQUE);

View File

@ -1047,18 +1047,66 @@ begin
end;
function TCocoaWidgetSet.GetSysColor(nIndex: Integer): DWORD;
function AverageColor(Color1, Color2: TColorRef): TColorRef; inline;
begin
if Color1 = Color2 then
Result := Color1
else
Result :=
(((Color1 and $FF) + (Color2 and $FF)) shr 1) and $FF or
(((((Color1 shr 8) and $FF) + ((Color2 shr 8) and $FF)) shr 1) and $FF) shl 8 or
(((((Color1 shr 16) and $FF) + ((Color2 shr 16) and $FF)) shr 1) and $FF) shl 16;
end;
var
Color, RGBColor: NSColor;
LocalPool: NSAutoReleasePool;
Color, RGBColor, PatternColor: NSColor;
ImageRep: NSImageRep;
x, y: Integer;
begin
Color := SysColorToNSColor(nIndex);
if Assigned(Color) then
begin
LocalPool := NSAutoReleasePool.alloc.init;
RGBColor := Color.colorUsingColorSpaceName(NSCalibratedRGBColorSpace);
// if color is a pattern it can't be converted
// if color is a pattern it can't be converted as is to a solid color value
if RGBColor = nil then
Result := 0
begin
PatternColor := Color.colorUsingColorSpaceName(NSPatternColorSpace);
if PatternColor = nil then
Result := 0
else
begin
// compute an average color of the top left 2x2 rectangle
ImageRep := PatternColor.patternImage.bestRepresentationForRect_context_hints(RectToNSRect(Types.Rect(0, 0, 1, 1)), nil, nil);
if (ImageRep = nil) or not ImageRep.isKindOfClass(NSBitmapImageRep) then
Result := 0
else
begin
for y := 0 to ImageRep.pixelsHigh - 1 do
for x := 0 to ImageRep.pixelsWide - 1 do
begin
RGBColor := NSBitmapImageRep(ImageRep).colorAtX_y(x, y).colorUsingColorSpaceName(NSCalibratedRGBColorSpace);
if Assigned(RGBColor) then
begin
if (x = 0) and (y = 0) then
Result := NSColorToRGB(RGBColor)
else
Result := AverageColor(Result, NSColorToRGB(RGBColor))
end
else
begin
Result := 0;
break;
end
end;
end;
end;
end
else
Result := NSColorToRGB(RGBColor);
LocalPool.release;
end
else
Result := 0;