From d5d685a580a6c946e496434ee81d332dca8768b8 Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Tue, 18 Aug 2020 02:23:04 +0200 Subject: [PATCH] Add an extensions for SKColorF (#1469) --- scripts/azure-pipelines.yml | 2 +- .../Extensions.cs | 18 +++++---- .../SkiaSharp.Views.Apple/AppleExtensions.cs | 37 +++++++++++++------ .../SkiaSharp.Views.AppleiOS/iOSExtensions.cs | 12 +++++- .../SkiaSharp.Views.Mac/MacExtensions.cs | 12 +++++- 5 files changed, 56 insertions(+), 25 deletions(-) diff --git a/scripts/azure-pipelines.yml b/scripts/azure-pipelines.yml index e2fd925e..c2a85131 100644 --- a/scripts/azure-pipelines.yml +++ b/scripts/azure-pipelines.yml @@ -23,7 +23,7 @@ variables: MONO_VERSION_MACOS: 6_8_0 MONO_VERSION_LINUX: stable-bionic/snapshots/6.8.0.123 XCODE_VERSION: 11.3.1 - DOTNET_VERSION: 3.1.x + DOTNET_VERSION: 3.1.302 CONFIGURATION: 'Release' VM_IMAGE_WINDOWS: windows-2019 VM_IMAGE_WINDOWS_PREVIOUS: vs2017-win2016 diff --git a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Shared/Extensions.cs b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Shared/Extensions.cs index d153f21f..bcf9ad83 100644 --- a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Shared/Extensions.cs +++ b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Shared/Extensions.cs @@ -55,14 +55,16 @@ namespace SkiaSharp.Views.Forms // Xamarin.Forms.Color - public static Color ToFormsColor(this SKColor color) - { - return new Color(color.Red / 255.0, color.Green / 255.0, color.Blue / 255.0, color.Alpha / 255.0); - } + public static Color ToFormsColor(this SKColor color) => + new Color(color.Red / 255.0, color.Green / 255.0, color.Blue / 255.0, color.Alpha / 255.0); - public static SKColor ToSKColor(this Color color) - { - return new SKColor((byte)(color.R * 255), (byte)(color.G * 255), (byte)(color.B * 255), (byte)(color.A * 255)); - } + public static Color ToFormsColor(this SKColorF color) => + new Color(color.Red, color.Green, color.Blue, color.Alpha); + + public static SKColor ToSKColor(this Color color) => + new SKColor((byte)(color.R * 255), (byte)(color.G * 255), (byte)(color.B * 255), (byte)(color.A * 255)); + + public static SKColorF ToSKColorF(this Color color) => + new SKColorF((float)color.R, (float)color.G, (float)color.B, (float)color.A); } } diff --git a/source/SkiaSharp.Views/SkiaSharp.Views.Apple/AppleExtensions.cs b/source/SkiaSharp.Views/SkiaSharp.Views.Apple/AppleExtensions.cs index 0aaa28c2..f054ba0d 100644 --- a/source/SkiaSharp.Views/SkiaSharp.Views.Apple/AppleExtensions.cs +++ b/source/SkiaSharp.Views/SkiaSharp.Views.Apple/AppleExtensions.cs @@ -62,10 +62,11 @@ namespace SkiaSharp.Views.Mac // CGColor - public static SKColor ToSKColor(this CGColor color) - { - return UIColor.FromCGColor(color).ToSKColor(); - } + public static SKColor ToSKColor(this CGColor color) => + UIColor.FromCGColor(color).ToSKColor(); + + public static SKColorF ToSKColorF(this CGColor color) => + UIColor.FromCGColor(color).ToSKColorF(); public static CGColor ToCGColor(this SKColor color) { @@ -77,19 +78,31 @@ namespace SkiaSharp.Views.Mac #endif } + public static CGColor ToCGColor(this SKColorF color) + { +#if __TVOS__ || __WATCHOS__ || __IOS__ + // see https://bugzilla.xamarin.com/show_bug.cgi?id=44507 + return UIColor.FromRGBA(color.Red, color.Green, color.Blue, color.Alpha).CGColor; +#else + return UIColor.FromRgba(color.Red, color.Green, color.Blue, color.Alpha).CGColor; +#endif + } + // CIColor #if !__WATCHOS__ - public static SKColor ToSKColor(this CIColor color) - { - return UIColor.FromCIColor(color).ToSKColor(); - } + public static SKColor ToSKColor(this CIColor color) => + UIColor.FromCIColor(color).ToSKColor(); - public static CIColor ToCIColor(this SKColor color) - { - return new CIColor(color.Red / 255f, color.Green / 255f, color.Blue / 255f, color.Alpha / 255f); - } + public static SKColorF ToSKColorF(this CIColor color) => + UIColor.FromCIColor(color).ToSKColorF(); + + public static CIColor ToCIColor(this SKColor color) => + new CIColor(color.Red / 255f, color.Green / 255f, color.Blue / 255f, color.Alpha / 255f); + + public static CIColor ToCIColor(this SKColorF color) => + new CIColor(color.Red, color.Green, color.Blue, color.Alpha); #endif diff --git a/source/SkiaSharp.Views/SkiaSharp.Views.AppleiOS/iOSExtensions.cs b/source/SkiaSharp.Views/SkiaSharp.Views.AppleiOS/iOSExtensions.cs index b7c0c8b4..c0ceaa25 100644 --- a/source/SkiaSharp.Views/SkiaSharp.Views.AppleiOS/iOSExtensions.cs +++ b/source/SkiaSharp.Views/SkiaSharp.Views.AppleiOS/iOSExtensions.cs @@ -20,11 +20,19 @@ namespace SkiaSharp.Views.iOS return new SKColor((byte)(r * 255), (byte)(g * 255), (byte)(b * 255), (byte)(a * 255)); } - public static UIColor ToUIColor(this SKColor color) + public static SKColorF ToSKColorF(this UIColor color) { - return UIColor.FromRGBA(color.Red, color.Green, color.Blue, color.Alpha); + System.nfloat r, g, b, a; + color.GetRGBA(out r, out g, out b, out a); + return new SKColorF((float)r, (float)g, (float)b, (float)a); } + public static UIColor ToUIColor(this SKColor color) => + UIColor.FromRGBA(color.Red, color.Green, color.Blue, color.Alpha); + + public static UIColor ToUIColor(this SKColorF color) => + UIColor.FromRGBA(color.Red, color.Green, color.Blue, color.Alpha); + // UIImage public static SKImage ToSKImage(this UIImage uiImage) diff --git a/source/SkiaSharp.Views/SkiaSharp.Views.Mac/MacExtensions.cs b/source/SkiaSharp.Views/SkiaSharp.Views.Mac/MacExtensions.cs index 1e7e1ada..581dbe00 100644 --- a/source/SkiaSharp.Views/SkiaSharp.Views.Mac/MacExtensions.cs +++ b/source/SkiaSharp.Views/SkiaSharp.Views.Mac/MacExtensions.cs @@ -14,11 +14,19 @@ namespace SkiaSharp.Views.Mac return new SKColor((byte)(r * 255), (byte)(g * 255), (byte)(b * 255), (byte)(a * 255)); } - public static NSColor ToNSColor(this SKColor color) + public static SKColorF ToSKColorF(this NSColor color) { - return NSColor.FromRgba(color.Red, color.Green, color.Blue, color.Alpha); + System.nfloat r, g, b, a; + color.GetRgba(out r, out g, out b, out a); + return new SKColorF((float)r, (float)g, (float)b, (float)a); } + public static NSColor ToNSColor(this SKColor color) => + NSColor.FromRgba(color.Red, color.Green, color.Blue, color.Alpha); + + public static NSColor ToNSColor(this SKColorF color) => + NSColor.FromRgba(color.Red, color.Green, color.Blue, color.Alpha); + // NSImage