public static PColor ToUIColor(this string hexColor) { if (string.IsNullOrWhiteSpace(hexColor)) { throw new ArgumentException("Invalid color string.", nameof(hexColor)); } if (!hexColor.StartsWith("#", StringComparison.Ordinal)) { hexColor = hexColor.Insert(0, "#"); } PColor color = PColor.Clear; switch (hexColor.Length) { case 9: { var cuint = Convert.ToUInt32(hexColor.Substring(1), 16); var a = (byte)(cuint >> 24); var r = (byte)((cuint >> 16) & 0xff); var g = (byte)((cuint >> 8) & 0xff); var b = (byte)(cuint & 0xff); #if __MACOS__ color = PColor.FromRgba(r, g, b, a); #elif __IOS__ color = PColor.FromRGBA(r, g, b, a); #endif break; } case 7: { var cuint = Convert.ToUInt32(hexColor.Substring(1), 16); var r = (byte)((cuint >> 16) & 0xff); var g = (byte)((cuint >> 8) & 0xff); var b = (byte)(cuint & 0xff); #if __MACOS__ color = PColor.FromRgba(r, g, b, (byte)255); #elif __IOS__ color = PColor.FromRGBA(r, g, b, (byte)255); #endif break; } case 5: { var cuint = Convert.ToUInt16(hexColor.Substring(1), 16); var a = (byte)(cuint >> 12); var r = (byte)((cuint >> 8) & 0xf); var g = (byte)((cuint >> 4) & 0xf); var b = (byte)(cuint & 0xf); a = (byte)(a << 4 | a); r = (byte)(r << 4 | r); g = (byte)(g << 4 | g); b = (byte)(b << 4 | b); #if __MACOS__ color = PColor.FromRgba(r, g, b, a); #elif __IOS__ color = PColor.FromRGBA(r, g, b, a); #endif break; } case 4: { var cuint = Convert.ToUInt16(hexColor.Substring(1), 16); var r = (byte)((cuint >> 8) & 0xf); var g = (byte)((cuint >> 4) & 0xf); var b = (byte)(cuint & 0xf); r = (byte)(r << 4 | r); g = (byte)(g << 4 | g); b = (byte)(b << 4 | b); #if __MACOS__ color = PColor.FromRgba(r, g, b, (byte)255); #elif __IOS__ color = PColor.FromRGBA(r, g, b, (byte)255); #endif break; } default: throw new FormatException(string.Format("The {0} string passed in the c argument is not a recognized Color format.", hexColor)); } return(color); }
public override UIImage ToNativeImage(CGSize targetSize, UIColor color = default(UIColor), Thickness margin = default(Thickness)) { if (bezierPath == null) { return(null); } CGSize imageSize = bezierPath.Bounds.Size; if ((int)imageSize.Width <= 0 && (int)imageSize.Height <= 0) { return(null); } if (nfloat.IsNaN(targetSize.Width) && nfloat.IsNaN(targetSize.Height)) { targetSize = new CGSize(0.85f * imageSize.Width, 0.85f * imageSize.Height); } else if (nfloat.IsNaN(targetSize.Width)) { targetSize.Width = (imageSize.Width / imageSize.Height) * targetSize.Height; } else if (nfloat.IsNaN(targetSize.Height)) { targetSize.Height = (imageSize.Height / imageSize.Width) * targetSize.Width; } if ((int)targetSize.Width <= 0 && (int)targetSize.Height <= 0) { return(null); } nfloat scale = 1f; var translate = CGPoint.Empty; if (!imageSize.Equals(targetSize)) { var scaleX = targetSize.Width / imageSize.Width; var scaleY = targetSize.Height / imageSize.Height; scale = (nfloat)Math.Min(targetSize.Width / imageSize.Width, targetSize.Height / imageSize.Height); if (scaleX > scaleY) { translate.X = (targetSize.Width - (imageSize.Width * scale)) * 0.5f; } else if (scaleX < scaleY) { translate.Y = (targetSize.Height - (imageSize.Height * scale)) * 0.5f; } } UIImage image; #if __IOS__ UIGraphics.BeginImageContextWithOptions(targetSize, false, 0); using (var context = UIGraphics.GetCurrentContext()) { context.TranslateCTM(translate.X, translate.Y); context.ScaleCTM(scale, scale); context.InterpolationQuality = CGInterpolationQuality.High; context.SetFillColor((color ?? UIColor.Black).CGColor); bezierPath.UsesEvenOddFillRule = (FillRule == FillRule.EvenOdd); bezierPath.Fill(); image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); } #elif __MACOS__ // macOS TODO image = null; #endif return(image); }
/// <remarks> /// This method is required for performance. Creating a native Color /// requires a round-trip with Objective-C, so updating this value only when opacity /// and color changes is more efficient. /// </remarks> partial void UpdateColorWithOpacity(Color newColor, double opacity) { newColor.A = (byte)(newColor.A * opacity); ColorWithOpacity = newColor; }