Skip to content

Commit

Permalink
Color: ToInt() → FromRGB()
Browse files Browse the repository at this point in the history
  • Loading branch information
swharden committed Jul 10, 2023
1 parent 48dca6b commit 03422b8
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 174 deletions.
2 changes: 1 addition & 1 deletion src/RasterSharp.Tests/BitmapIOTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void Test_Write_Bitmap()
image.Red.Save(Path.Join(outputFolder, "save-red.bmp"));
image.Green.Save(Path.Join(outputFolder, "save-green.bmp"));
image.Blue.Save(Path.Join(outputFolder, "save-blue.bmp"));
image.Save(Path.Join(outputFolder, "save-rgb.bmp"));
image.SaveBmp(Path.Join(outputFolder, "save-rgb.bmp"));

Image image2 = new(Path.Join(outputFolder, "save-rgb.bmp"));
for (int y = 0; y < image.Height; y++)
Expand Down
2 changes: 1 addition & 1 deletion src/RasterSharp.Tests/ColorConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void Test_Color_RGBA()
{
int original = rand.Next();
(byte r, byte g, byte b, byte a) = Color.Bytes(original);
int returned = Color.ToInt(r, g, b, a);
int returned = Color.FromRGBA(r, g, b, a);
Assert.That(returned, Is.EqualTo(original));
}
}
Expand Down
22 changes: 16 additions & 6 deletions src/RasterSharp/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@ public static int Mix(int colorA, int colorB, double fractionB = 0.5)
byte b = (byte)(bytesA.b * fractionA + bytesB.b * fractionB);
byte a = (byte)(bytesA.a * fractionA + bytesB.a * fractionB);

return ToInt(r, g, b, a);
return FromRGBA(r, g, b, a);
}

public static int RandomColor(Random rand, byte min = 100, byte max = 255, byte alpha = 0)
public static int RandomColor(Random rand, byte min = 0, byte max = 255, byte alpha = 0)
{
byte r = (byte)rand.Next(min, max);
byte g = (byte)rand.Next(min, max);
byte b = (byte)rand.Next(min, max);
return ToInt(r, g, b, alpha);
return FromRGBA(r, g, b, alpha);
}

public static int ToInt(byte r, byte g, byte b, byte a = 0)
public static int FromRGB(byte r, byte g, byte b)
{
return FromRGBA(r, g, b, 0);
}

public static int FromRGBA(byte r, byte g, byte b, byte a)
{
return (r << 24) | (g << 16) | (b << 8) | (a << 0);
}
Expand All @@ -47,7 +52,7 @@ public static int Fractional(double r, double g, double b, double a = 0)
byte g2 = (byte)(g * 255);
byte b2 = (byte)(b * 255);
byte a2 = (byte)(a * 255);
return ToInt(r2, g2, b2, a2);
return FromRGBA(r2, g2, b2, a2);
}

public static int Replace(int color, byte? red = null, byte? green = null, byte? blue = null, byte? alpha = null)
Expand All @@ -57,6 +62,11 @@ public static int Replace(int color, byte? red = null, byte? green = null, byte?
g = green ?? g;
b = blue ?? b;
a = alpha ?? a;
return ToInt(r, g, b, a);
return FromRGBA(r, g, b, a);
}

public static int From(System.Drawing.Color color)
{
return FromRGBA(color.R, color.G, color.B, color.A);
}
}
2 changes: 1 addition & 1 deletion src/RasterSharp/Colormaps/Grayscale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public int GetColor(double fraction)
fraction = Math.Max(0, fraction);
fraction = Math.Min(1, fraction);
byte value = (byte)(fraction * 255);
return Color.ToInt(value, value, value);
return Color.FromRGB(value, value, value);
}
}
2 changes: 1 addition & 1 deletion src/RasterSharp/Colormaps/Viridis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int GetColor(double fraction)
byte g = (byte)(value2 >> 8);
byte b = (byte)(value2 >> 0);

return Color.ToInt(r, g, b, a);
return Color.FromRGBA(r, g, b, a);
}

private static readonly uint[] rgbs =
Expand Down
298 changes: 149 additions & 149 deletions src/RasterSharp/Colors.cs

Large diffs are not rendered by default.

55 changes: 40 additions & 15 deletions src/RasterSharp/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public int GetRGBA(int x, int y)
byte g = Green.GetByte(x, y);
byte b = Blue.GetByte(x, y);
byte a = Alpha.GetByte(x, y);
return Color.ToInt(r, g, b, a);
return Color.FromRGBA(r, g, b, a);
}

public void SetRGBA(int x, int y, int rgba)
Expand All @@ -123,23 +123,33 @@ public byte[] GetBitmapBytes()
return BitmapIO.GetBitmapBytes(this);
}

public void Save(string path)
public void SaveBmp(string path)
{
if (!path.EndsWith(".bmp", StringComparison.InvariantCultureIgnoreCase))
throw new InvalidOperationException("filename must end with .bmp");

System.IO.File.WriteAllBytes(path, GetBitmapBytes());
}

public void DrawRectangle(Point pt, int radius, int color)
public void Fill(int color = 0)
{
Rectangle rect = new(
x: pt.X - radius,
y: pt.Y - radius,
width: radius * 2,
height: radius * 2);
Rectangle rect = new(0, 0, Width, Height);
FillRectangle(rect, color);
}

DrawRectangle(rect, color);
public void FillRectangle(int x, int y, int width, int height, int color)
{
Rectangle rect = new(x, y, width, height);
FillRectangle(rect, color);
}

public void FillRectangle(Rectangle rect, int color)
{
var colors = Color.Bytes(color);
Red.FillRectangle(rect, colors.r);
Green.FillRectangle(rect, colors.g);
Blue.FillRectangle(rect, colors.b);
Alpha.FillRectangle(rect, colors.a);
}

public void FillRectangle(Point pt, int radius, int color)
Expand All @@ -153,13 +163,10 @@ public void FillRectangle(Point pt, int radius, int color)
FillRectangle(rect, color);
}

public void FillRectangle(Rectangle rect, int color)
public void DrawRectangle(int x, int y, int width, int height, int color)
{
var colors = Color.Bytes(color);
Red.FillRectangle(rect, colors.r);
Green.FillRectangle(rect, colors.g);
Blue.FillRectangle(rect, colors.b);
Alpha.FillRectangle(rect, colors.a);
Rectangle rect = new(x, y, width, height);
DrawRectangle(rect, color);
}

public void DrawRectangle(Rectangle rect, int color)
Expand All @@ -171,6 +178,24 @@ public void DrawRectangle(Rectangle rect, int color)
Alpha.DrawRectangle(rect, colors.a);
}

public void DrawRectangle(Point pt, int radius, int color)
{
Rectangle rect = new(
x: pt.X - radius,
y: pt.Y - radius,
width: radius * 2,
height: radius * 2);

DrawRectangle(rect, color);
}

public void DrawLine(int x1, int y1, int x2, int y2, int color)
{
Point pt1 = new(x1, y1);
Point pt2 = new(x2, y2);
DrawLine(pt1, pt2, color);
}

public void DrawLine(Point pt1, Point pt2, int color)
{
var colors = Color.Bytes(color);
Expand Down

0 comments on commit 03422b8

Please sign in to comment.