mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-09-07 09:00:35 +02:00
cocoa: implement GetSysColor for pattern colors like clWindow and clBtnFace
git-svn-id: trunk@34391 -
This commit is contained in:
parent
02a0fa9668
commit
f1976687d0
@ -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);
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user