Browse Source

basic Color methods

af/merge-core
Anton Firszov 7 years ago
parent
commit
2e53d72c49
  1. 51
      src/ImageSharp/Color.cs
  2. 245
      tests/ImageSharp.Tests/ColorTests.cs

51
src/ImageSharp/Color.cs

@ -7,9 +7,9 @@ using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp
{
public struct Color
public readonly struct Color
{
private Rgba64 data;
private readonly Rgba64 data;
public Color(Rgba64 pixel)
{
@ -21,12 +21,55 @@ namespace SixLabors.ImageSharp
this.data = new Rgba64(pixel);
}
public Color(Argb32 pixel)
{
this.data = new Rgba64(pixel);
}
public Color(Bgra32 pixel)
{
this.data = new Rgba64(pixel);
}
public Color(Rgb24 pixel)
{
this.data = new Rgba64(pixel);
}
public Color(Bgr24 pixel)
{
this.data = new Rgba64(pixel);
}
public Color(Vector4 vector)
{
this.data = default;
this.data.FromVector4(vector);
this.data = new Rgba64(vector);
}
public static implicit operator Color(Rgba64 source) => new Color(source);
public static implicit operator Color(Rgba32 source) => new Color(source);
public static implicit operator Color(Bgra32 source) => new Color(source);
public static implicit operator Color(Argb32 source) => new Color(source);
public static implicit operator Color(Rgb24 source) => new Color(source);
public static implicit operator Color(Bgr24 source) => new Color(source);
public static implicit operator Rgba64(Color color) => color.data;
public static implicit operator Rgba32(Color color) => color.data.ToRgba32();
public static implicit operator Bgra32(Color color) => color.data.ToBgra32();
public static implicit operator Argb32(Color color) => color.data.ToArgb32();
public static implicit operator Rgb24(Color color) => color.data.ToRgb24();
public static implicit operator Bgr24(Color color) => color.data.ToBgr24();
public static Color FromRgba(byte r, byte g, byte b, byte a) => new Color(new Rgba32(r, g, b, a));
public TPixel ToPixel<TPixel>()

245
tests/ImageSharp.Tests/ColorTests.cs

@ -1,10 +1,255 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.PixelFormats;
using Xunit;
namespace SixLabors.ImageSharp.Tests
{
public class ColorTests
{
public class ConstructFrom
{
[Fact]
public void Rgba64()
{
Rgba64 source = new Rgba64(100, 2222, 3333, 4444);
// Act:
Color color = new Color(source);
// Assert:
Rgba64 data = color.ToPixel<Rgba64>();
Assert.Equal(source, data);
}
[Fact]
public void Rgba32()
{
Rgba32 source = new Rgba32(1, 22, 33, 231);
// Act:
Color color = new Color(source);
// Assert:
Rgba32 data = color.ToPixel<Rgba32>();
Assert.Equal(source, data);
}
[Fact]
public void Argb32()
{
Argb32 source = new Argb32(1, 22, 33, 231);
// Act:
Color color = new Color(source);
// Assert:
Argb32 data = color.ToPixel<Argb32>();
Assert.Equal(source, data);
}
[Fact]
public void Bgra32()
{
Bgra32 source = new Bgra32(1, 22, 33, 231);
// Act:
Color color = new Color(source);
// Assert:
Bgra32 data = color.ToPixel<Bgra32>();
Assert.Equal(source, data);
}
[Fact]
public void Rgb24()
{
Rgb24 source = new Rgb24(1, 22, 231);
// Act:
Color color = new Color(source);
// Assert:
Rgb24 data = color.ToPixel<Rgb24>();
Assert.Equal(source, data);
}
[Fact]
public void Bgr24()
{
Bgr24 source = new Bgr24(1, 22, 231);
// Act:
Color color = new Color(source);
// Assert:
Bgr24 data = color.ToPixel<Bgr24>();
Assert.Equal(source, data);
}
}
public class Cast
{
[Fact]
public void Rgba64()
{
Rgba64 source = new Rgba64(100, 2222, 3333, 4444);
// Act:
Color color = source;
// Assert:
Rgba64 data = color.ToPixel<Rgba64>();
Assert.Equal(source, data);
}
[Fact]
public void Rgba32()
{
Rgba32 source = new Rgba32(1, 22, 33, 231);
// Act:
Color color = source;
// Assert:
Rgba32 data = color.ToPixel<Rgba32>();
Assert.Equal(source, data);
}
[Fact]
public void Argb32()
{
Argb32 source = new Argb32(1, 22, 33, 231);
// Act:
Color color = source;
// Assert:
Argb32 data = color.ToPixel<Argb32>();
Assert.Equal(source, data);
}
[Fact]
public void Bgra32()
{
Bgra32 source = new Bgra32(1, 22, 33, 231);
// Act:
Color color = source;
// Assert:
Bgra32 data = color.ToPixel<Bgra32>();
Assert.Equal(source, data);
}
[Fact]
public void Rgb24()
{
Rgb24 source = new Rgb24(1, 22, 231);
// Act:
Color color = source;
// Assert:
Rgb24 data = color.ToPixel<Rgb24>();
Assert.Equal(source, data);
}
[Fact]
public void Bgr24()
{
Bgr24 source = new Bgr24(1, 22, 231);
// Act:
Color color = source;
// Assert:
Bgr24 data = color.ToPixel<Bgr24>();
Assert.Equal(source, data);
}
}
public class CastTo
{
[Fact]
public void Rgba64()
{
Rgba64 source = new Rgba64(100, 2222, 3333, 4444);
// Act:
Color color = new Color(source);
// Assert:
Rgba64 data = color;
Assert.Equal(source, data);
}
[Fact]
public void Rgba32()
{
Rgba32 source = new Rgba32(1, 22, 33, 231);
// Act:
Color color = new Color(source);
// Assert:
Rgba32 data = color;
Assert.Equal(source, data);
}
[Fact]
public void Argb32()
{
Argb32 source = new Argb32(1, 22, 33, 231);
// Act:
Color color = new Color(source);
// Assert:
Argb32 data = color;
Assert.Equal(source, data);
}
[Fact]
public void Bgra32()
{
Bgra32 source = new Bgra32(1, 22, 33, 231);
// Act:
Color color = new Color(source);
// Assert:
Bgra32 data = color;
Assert.Equal(source, data);
}
[Fact]
public void Rgb24()
{
Rgb24 source = new Rgb24(1, 22, 231);
// Act:
Color color = new Color(source);
// Assert:
Rgb24 data = color;
Assert.Equal(source, data);
}
[Fact]
public void Bgr24()
{
Bgr24 source = new Bgr24(1, 22, 231);
// Act:
Color color = new Color(source);
// Assert:
Bgr24 data = color;
Assert.Equal(source, data);
}
}
}
}
Loading…
Cancel
Save