Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #67 from dotnet/dev/native-primitives
Browse files Browse the repository at this point in the history
Add more native conversions
  • Loading branch information
jonlipsky authored Apr 20, 2021
2 parents 49a66b2 + 200f268 commit b8b09e9
Show file tree
Hide file tree
Showing 8 changed files with 709 additions and 757 deletions.
42 changes: 20 additions & 22 deletions src/Microsoft.Maui.Graphics/Android/GraphicsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,6 @@ public static class GraphicsExtensions
return color;
}

public static int ToArgb(this Color target)
{
var a = (int) (target.Alpha * 255f);
var r = (int) (target.Red * 255f);
var g = (int) (target.Green * 255f);
var b = (int) (target.Blue * 255f);

int argb = a << 24 | r << 16 | g << 8 | b;
return argb;
}

public static int ToArgb(this Color target, float alpha)
{
var a = (int) (target.Alpha * 255f * alpha);
var r = (int) (target.Red * 255f);
var g = (int) (target.Green * 255f);
var b = (int) (target.Blue * 255f);

int argb = a << 24 | r << 16 | g << 8 | b;
return argb;
}

public static global::Android.Graphics.Color AsColor(this Color target)
{
var r = (int) (target.Red * 255f);
Expand Down Expand Up @@ -72,6 +50,11 @@ public static RectangleF AsRectangleF(this RectF target)
return new RectangleF(target.Left, target.Top, Math.Abs(target.Width()), Math.Abs(target.Height()));
}

public static Rectangle AsRectangle(this RectF target)
{
return new Rectangle(target.Left, target.Top, Math.Abs(target.Width()), Math.Abs(target.Height()));
}

public static RectF AsRectF(this Rect target)
{
return new RectF(target);
Expand Down Expand Up @@ -339,6 +322,21 @@ public static PointF AsPointF(this global::Android.Graphics.PointF target)
return new PointF(target.X, target.Y);
}

public static Point AsPoint(this global::Android.Graphics.PointF target)
{
return new Point(target.X, target.Y);
}

public static SizeF AsSizeF(this global::Android.Util.SizeF target)
{
return new SizeF(target.Width, target.Height);
}

public static Size AsSize(this global::Android.Util.SizeF target)
{
return new Size(target.Width, target.Height);
}

public static Bitmap GetPatternBitmap(this Paint paint, float scale = 1)
{
var pattern = paint?.Pattern;
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.Maui.Graphics/Android/NativeCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public override void SetFillPaint(Paint paint, float x1, float y1, float x2, flo

for (int i = 0; i < vStops.Length; i++)
{
colors[i] = vStops[i].Color.ToArgb(CurrentState.Alpha);
colors[i] = vStops[i].Color.MultiplyAlpha(CurrentState.Alpha).ToInt();
stops[i] = vStops[i].Offset;
}

Expand All @@ -303,7 +303,7 @@ public override void SetFillPaint(Paint paint, float x1, float y1, float x2, flo

for (int i = 0; i < vStops.Length; i++)
{
colors[i] = vStops[i].Color.ToArgb(CurrentState.Alpha);
colors[i] = vStops[i].Color.MultiplyAlpha(CurrentState.Alpha).ToInt();
stops[i] = vStops[i].Offset;
}

Expand Down
Loading

0 comments on commit b8b09e9

Please sign in to comment.