Browse Source

Merge branch 'af/general-color-type' into af/refactor-drawing

# Conflicts:
#	src/ImageSharp/Color/Color.Conversions.cs
af/merge-core
Anton Firszov 7 years ago
parent
commit
5220f0d467
  1. 1
      ImageSharp.sln.DotSettings
  2. 133
      src/ImageSharp/Color/Color.Conversions.cs
  3. 39
      src/ImageSharp/Color/Color.cs
  4. 16
      src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs
  5. 16
      src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs
  6. 16
      src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs
  7. 16
      src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs
  8. 16
      src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs
  9. 23
      src/ImageSharp/PixelFormats/PixelImplementations/Rgba64.cs
  10. 2
      tests/ImageSharp.Tests/Color/ColorTests.cs

1
ImageSharp.sln.DotSettings

@ -388,5 +388,6 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Bgra/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Quantizer/@EntryIndexedValue">True</s:Boolean>
</wpf:ResourceDictionary>

133
src/ImageSharp/Color/Color.Conversions.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.PixelFormats;
@ -16,107 +17,52 @@ namespace SixLabors.ImageSharp
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="pixel">The <see cref="Rgba64"/> containing the color information.</param>
public Color(Rgba64 pixel)
{
this.data = pixel;
}
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Rgba64 pixel) => this.data = pixel;
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="pixel">The <see cref="Rgba32"/> containing the color information.</param>
public Color(Rgba32 pixel)
{
this.data = new Rgba64(pixel);
}
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Rgba32 pixel) => this.data = new Rgba64(pixel);
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="pixel">The <see cref="Argb32"/> containing the color information.</param>
public Color(Argb32 pixel)
{
this.data = new Rgba64(pixel);
}
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Argb32 pixel) => this.data = new Rgba64(pixel);
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="pixel">The <see cref="Bgra32"/> containing the color information.</param>
public Color(Bgra32 pixel)
{
this.data = new Rgba64(pixel);
}
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Bgra32 pixel) => this.data = new Rgba64(pixel);
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="pixel">The <see cref="Rgb24"/> containing the color information.</param>
public Color(Rgb24 pixel)
{
this.data = new Rgba64(pixel);
}
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Rgb24 pixel) => this.data = new Rgba64(pixel);
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="pixel">The <see cref="Bgr24"/> containing the color information.</param>
public Color(Bgr24 pixel)
{
this.data = new Rgba64(pixel);
}
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Bgr24 pixel) => this.data = new Rgba64(pixel);
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="vector">The <see cref="Vector4"/> containing the color information.</param>
public Color(Vector4 vector)
{
this.data = new Rgba64(vector);
}
/// <summary>
/// Converts an <see cref="Rgba64"/> to <see cref="Color"/>.
/// </summary>
/// <param name="source">The <see cref="Rgba64"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
public static implicit operator Color(Rgba64 source) => new Color(source);
/// <summary>
/// Converts an <see cref="Rgba32"/> to <see cref="Color"/>.
/// </summary>
/// <param name="source">The <see cref="Rgba32"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
public static implicit operator Color(Rgba32 source) => new Color(source);
/// <summary>
/// Converts an <see cref="Bgra32"/> to <see cref="Color"/>.
/// </summary>
/// <param name="source">The <see cref="Bgra32"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
public static implicit operator Color(Bgra32 source) => new Color(source);
/// <summary>
/// Converts an <see cref="Argb32"/> to <see cref="Color"/>.
/// </summary>
/// <param name="source">The <see cref="Argb32"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
public static implicit operator Color(Argb32 source) => new Color(source);
/// <summary>
/// Converts an <see cref="Rgb24"/> to <see cref="Color"/>.
/// </summary>
/// <param name="source">The <see cref="Rgb24"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
public static implicit operator Color(Rgb24 source) => new Color(source);
/// <summary>
/// Converts an <see cref="Bgr24"/> to <see cref="Color"/>.
/// </summary>
/// <param name="source">The <see cref="Bgr24"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
public static implicit operator Color(Bgr24 source) => new Color(source);
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Vector4 vector) => this.data = new Rgba64(vector);
[MethodImpl(InliningOptions.ShortMethod)]
/// <summary>
/// Converts an <see cref="Vector4"/> to <see cref="Color"/>.
/// </summary>
@ -124,47 +70,20 @@ namespace SixLabors.ImageSharp
/// <returns>The <see cref="Color"/>.</returns>
public static explicit operator Color(Vector4 source) => new Color(source);
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Rgba64"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Rgba64"/>.</returns>
public static implicit operator Rgba64(Color color) => color.data;
[MethodImpl(InliningOptions.ShortMethod)]
internal Rgba32 ToRgba32() => this.data.ToRgba32();
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Rgba32"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Rgba32"/>.</returns>
public static implicit operator Rgba32(Color color) => color.data.ToRgba32();
[MethodImpl(InliningOptions.ShortMethod)]
internal Bgra32 ToBgra32() => this.data.ToBgra32();
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Bgra32"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Bgra32"/>.</returns>
public static implicit operator Bgra32(Color color) => color.data.ToBgra32();
[MethodImpl(InliningOptions.ShortMethod)]
internal Argb32 ToArgb32() => this.data.ToArgb32();
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Argb32"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Argb32"/>.</returns>
public static implicit operator Argb32(Color color) => color.data.ToArgb32();
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Rgb24"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Rgb24"/>.</returns>
public static implicit operator Rgb24(Color color) => color.data.ToRgb24();
[MethodImpl(InliningOptions.ShortMethod)]
internal Rgb24 ToRgb24() => this.data.ToRgb24();
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Bgr24"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Bgr24"/>.</returns>
public static implicit operator Bgr24(Color color) => color.data.ToBgr24();
[MethodImpl(InliningOptions.ShortMethod)]
internal Bgr24 ToBgr24() => this.data.ToBgr24();
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Vector4"/>.

39
src/ImageSharp/Color/Color.cs

@ -5,6 +5,7 @@ using System;
using System.Buffers.Binary;
using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.PixelFormats;
@ -24,6 +25,26 @@ namespace SixLabors.ImageSharp
{
private readonly Rgba64 data;
[MethodImpl(InliningOptions.ShortMethod)]
private Color(byte r, byte g, byte b, byte a)
{
this.data = new Rgba64(
ImageMaths.UpscaleFrom8BitTo16Bit(r),
ImageMaths.UpscaleFrom8BitTo16Bit(g),
ImageMaths.UpscaleFrom8BitTo16Bit(b),
ImageMaths.UpscaleFrom8BitTo16Bit(a));
}
[MethodImpl(InliningOptions.ShortMethod)]
private Color(byte r, byte g, byte b)
{
this.data = new Rgba64(
ImageMaths.UpscaleFrom8BitTo16Bit(r),
ImageMaths.UpscaleFrom8BitTo16Bit(g),
ImageMaths.UpscaleFrom8BitTo16Bit(b),
ushort.MaxValue);
}
/// <summary>
/// Checks whether two <see cref="Color"/> structures are equal.
/// </summary>
@ -33,6 +54,7 @@ namespace SixLabors.ImageSharp
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter;
/// otherwise, false.
/// </returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator ==(Color left, Color right)
{
return left.Equals(right);
@ -47,6 +69,7 @@ namespace SixLabors.ImageSharp
/// True if the <paramref name="left"/> parameter is not equal to the <paramref name="right"/> parameter;
/// otherwise, false.
/// </returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static bool operator !=(Color left, Color right)
{
return !left.Equals(right);
@ -60,7 +83,8 @@ namespace SixLabors.ImageSharp
/// <param name="b">The blue component (0-255).</param>
/// <param name="a">The alpha component (0-255).</param>
/// <returns>The <see cref="Color"/>.</returns>
public static Color FromRgba(byte r, byte g, byte b, byte a) => new Color(new Rgba32(r, g, b, a));
[MethodImpl(InliningOptions.ShortMethod)]
public static Color FromRgba(byte r, byte g, byte b, byte a) => new Color(r, g, b, a);
/// <summary>
/// Creates a <see cref="Color"/> from RGB bytes.
@ -69,7 +93,8 @@ namespace SixLabors.ImageSharp
/// <param name="g">The green component (0-255).</param>
/// <param name="b">The blue component (0-255).</param>
/// <returns>The <see cref="Color"/>.</returns>
public static Color FromRgb(byte r, byte g, byte b) => FromRgba(r, g, b, 255);
[MethodImpl(InliningOptions.ShortMethod)]
public static Color FromRgb(byte r, byte g, byte b) => new Color(r, g, b);
/// <summary>
/// Creates a new <see cref="Color"/> instance from the string representing a color in hexadecimal form.
@ -110,6 +135,7 @@ namespace SixLabors.ImageSharp
/// Gets the hexadecimal representation of the color instance in rrggbbaa form.
/// </summary>
/// <returns>A hexadecimal string representation of the value.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public string ToHex() => this.data.ToRgba32().ToHex();
/// <inheritdoc />
@ -121,6 +147,7 @@ namespace SixLabors.ImageSharp
/// </summary>
/// <typeparam name="TPixel">The pixel type to convert to.</typeparam>
/// <returns>The pixel value.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public TPixel ToPixel<TPixel>()
where TPixel : struct, IPixel<TPixel>
{
@ -130,6 +157,7 @@ namespace SixLabors.ImageSharp
}
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public bool Equals(Color other)
{
return this.data.PackedValue == other.data.PackedValue;
@ -138,15 +166,11 @@ namespace SixLabors.ImageSharp
/// <inheritdoc />
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is Color other && this.Equals(other);
}
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public override int GetHashCode()
{
return this.data.PackedValue.GetHashCode();
@ -155,6 +179,7 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Bulk convert a span of <see cref="Color"/> to a span of a specified pixel type.
/// </summary>
[MethodImpl(InliningOptions.ShortMethod)]
internal static void ToPixel<TPixel>(
Configuration configuration,
ReadOnlySpan<Color> source,

16
src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs

@ -142,6 +142,22 @@ namespace SixLabors.ImageSharp.PixelFormats
set => this.Argb = value;
}
/// <summary>
/// Converts an <see cref="Argb32"/> to <see cref="Color"/>.
/// </summary>
/// <param name="source">The <see cref="Argb32"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Color(Argb32 source) => new Color(source);
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Argb32"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Argb32"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Argb32(Color color) => color.ToArgb32();
/// <summary>
/// Compares two <see cref="Argb32"/> objects for equality.
/// </summary>

16
src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs

@ -50,6 +50,22 @@ namespace SixLabors.ImageSharp.PixelFormats
this.B = b;
}
/// <summary>
/// Converts an <see cref="Bgr24"/> to <see cref="Color"/>.
/// </summary>
/// <param name="source">The <see cref="Bgr24"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Color(Bgr24 source) => new Color(source);
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Bgr24"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Bgr24"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Bgr24(Color color) => color.ToBgr24();
/// <summary>
/// Compares two <see cref="Bgr24"/> objects for equality.
/// </summary>

16
src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs

@ -98,6 +98,22 @@ namespace SixLabors.ImageSharp.PixelFormats
set => this.Bgra = value;
}
/// <summary>
/// Converts an <see cref="Bgra32"/> to <see cref="Color"/>.
/// </summary>
/// <param name="source">The <see cref="Bgra32"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Color(Bgra32 source) => new Color(source);
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Bgra32"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Bgra32"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Bgra32(Color color) => color.ToBgra32();
/// <summary>
/// Compares two <see cref="Bgra32"/> objects for equality.
/// </summary>

16
src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs

@ -53,6 +53,22 @@ namespace SixLabors.ImageSharp.PixelFormats
this.B = b;
}
/// <summary>
/// Converts an <see cref="Rgb24"/> to <see cref="Color"/>.
/// </summary>
/// <param name="source">The <see cref="Rgb24"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Color(Rgb24 source) => new Color(source);
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Rgb24"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Rgb24"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Rgb24(Color color) => color.ToRgb24();
/// <summary>
/// Allows the implicit conversion of an instance of <see cref="ColorSpaces.Rgb"/> to a
/// <see cref="Rgb24"/>.

16
src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs

@ -169,6 +169,22 @@ namespace SixLabors.ImageSharp.PixelFormats
set => this.Rgba = value;
}
/// <summary>
/// Converts an <see cref="Rgba32"/> to <see cref="Color"/>.
/// </summary>
/// <param name="source">The <see cref="Rgba32"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Color(Rgba32 source) => new Color(source);
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Rgba32"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Rgba32"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Rgba32(Color color) => color.ToRgba32();
/// <summary>
/// Allows the implicit conversion of an instance of <see cref="ColorSpaces.Rgb"/> to a
/// <see cref="Rgba32"/>.

23
src/ImageSharp/PixelFormats/PixelImplementations/Rgba64.cs

@ -46,6 +46,7 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
/// <param name="a">The alpha component.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba64(ushort r, ushort g, ushort b, ushort a)
{
this.R = r;
@ -58,6 +59,7 @@ namespace SixLabors.ImageSharp.PixelFormats
/// Initializes a new instance of the <see cref="Rgba64"/> struct.
/// </summary>
/// <param name="source">A structure of 4 bytes in RGBA byte order.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba64(Rgba32 source)
{
this.R = ImageMaths.UpscaleFrom8BitTo16Bit(source.R);
@ -70,6 +72,7 @@ namespace SixLabors.ImageSharp.PixelFormats
/// Initializes a new instance of the <see cref="Rgba64"/> struct.
/// </summary>
/// <param name="source">A structure of 4 bytes in BGRA byte order.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba64(Bgra32 source)
{
this.R = ImageMaths.UpscaleFrom8BitTo16Bit(source.R);
@ -82,6 +85,7 @@ namespace SixLabors.ImageSharp.PixelFormats
/// Initializes a new instance of the <see cref="Rgba64"/> struct.
/// </summary>
/// <param name="source">A structure of 4 bytes in ARGB byte order.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba64(Argb32 source)
{
this.R = ImageMaths.UpscaleFrom8BitTo16Bit(source.R);
@ -94,6 +98,7 @@ namespace SixLabors.ImageSharp.PixelFormats
/// Initializes a new instance of the <see cref="Rgba64"/> struct.
/// </summary>
/// <param name="source">A structure of 3 bytes in RGB byte order.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba64(Rgb24 source)
{
this.R = ImageMaths.UpscaleFrom8BitTo16Bit(source.R);
@ -106,6 +111,7 @@ namespace SixLabors.ImageSharp.PixelFormats
/// Initializes a new instance of the <see cref="Rgba64"/> struct.
/// </summary>
/// <param name="source">A structure of 3 bytes in BGR byte order.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba64(Bgr24 source)
{
this.R = ImageMaths.UpscaleFrom8BitTo16Bit(source.R);
@ -118,6 +124,7 @@ namespace SixLabors.ImageSharp.PixelFormats
/// Initializes a new instance of the <see cref="Rgba64"/> struct.
/// </summary>
/// <param name="vector">The <see cref="Vector4"/>.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba64(Vector4 vector)
{
vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * Max;
@ -149,6 +156,22 @@ namespace SixLabors.ImageSharp.PixelFormats
set => Unsafe.As<Rgba64, ulong>(ref this) = value;
}
/// <summary>
/// Converts an <see cref="Rgba64"/> to <see cref="Color"/>.
/// </summary>
/// <param name="source">The <see cref="Rgba64"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Color(Rgba64 source) => new Color(source);
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Rgba64"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Rgba64"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Rgba64(Color color) => color.ToPixel<Rgba64>();
/// <summary>
/// Compares two <see cref="Rgba64"/> objects for equality.
/// </summary>

2
tests/ImageSharp.Tests/Color/ColorTests.cs

@ -49,6 +49,8 @@ namespace SixLabors.ImageSharp.Tests
Assert.False(c1 == c2);
Assert.True(c1 != c2);
Assert.False(c1.Equals(null));
}
[Fact]

Loading…
Cancel
Save