Browse Source

Add Compand methods to Color

This will eventually replace the PixelOperations methods.


Former-commit-id: a1058efb148c3fee54c69000698c96ade0c99c4f
Former-commit-id: 3704335fe924758d8d615ddc377f06b061308f0b
Former-commit-id: cb03cf38dab552cf2677fcfd8396f19906b80f1d
pull/17/head
James South 10 years ago
parent
commit
c734bfddcc
  1. 72
      src/ImageProcessor/Colors/Color.cs
  2. 1
      src/ImageProcessor/Common/Helpers/PixelOperations.cs
  3. 2
      src/ImageProcessor/project.lock.json.REMOVED.git-id

72
src/ImageProcessor/Colors/Color.cs

@ -473,6 +473,40 @@ namespace ImageProcessor
return (from * (1 - amount)) + (to * amount);
}
/// <summary>
/// Compresseses a linear color signal to its sRGB equivalent.
/// <see href="http://www.4p8.com/eric.brasseur/gamma.html#formulas"/>
/// <see href="http://entropymine.com/imageworsener/srgbformula/"/>
/// </summary>
/// <param name="linear">The <see cref="Color"/> whos signal to compress.</param>
/// <returns>The <see cref="Color"/>.</returns>
public static Color Compand(Color linear)
{
// TODO: Is there a faster way to do this?
float r = Compand(linear.R);
float g = Compand(linear.G);
float b = Compand(linear.B);
return new Color(r, g, b, linear.A);
}
/// <summary>
/// Expands an sRGB color signal to its linear equivalent.
/// <see href="http://www.4p8.com/eric.brasseur/gamma.html#formulas"/>
/// <see href="http://entropymine.com/imageworsener/srgbformula/"/>
/// </summary>
/// <param name="gamma">The <see cref="Color"/> whos signal to expand.</param>
/// <returns>The <see cref="Color"/>.</returns>
public static Color InverseCompand(Color gamma)
{
// TODO: Is there a faster way to do this?
float r = InverseCompand(gamma.R);
float g = InverseCompand(gamma.G);
float b = InverseCompand(gamma.B);
return new Color(r, g, b, gamma.A);
}
/// <summary>
/// Gets a <see cref="Vector4"/> representation for this <see cref="Color"/>.
/// </summary>
@ -527,6 +561,44 @@ namespace ImageProcessor
return this.backingVector.Equals(other.backingVector);
}
/// <summary>
/// Gets the compressed sRGB value from an linear signal.
/// <see href="http://www.4p8.com/eric.brasseur/gamma.html#formulas"/>
/// <see href="http://entropymine.com/imageworsener/srgbformula/"/>
/// </summary>
/// <param name="signal">The signal value to compress.</param>
/// <returns>
/// The <see cref="float"/>.
/// </returns>
private static float Compand(float signal)
{
if (signal <= 0.0031308f)
{
return signal * 12.92f;
}
return (1.055f * (float)Math.Pow(signal, 0.41666666f)) - 0.055f;
}
/// <summary>
/// Gets the expanded linear value from an sRGB signal.
/// <see href="http://www.4p8.com/eric.brasseur/gamma.html#formulas"/>
/// <see href="http://entropymine.com/imageworsener/srgbformula/"/>
/// </summary>
/// <param name="signal">The signal value to expand.</param>
/// <returns>
/// The <see cref="float"/>.
/// </returns>
private static float InverseCompand(float signal)
{
if (signal <= 0.04045f)
{
return signal / 12.92f;
}
return (float)Math.Pow((signal + 0.055f) / 1.055f, 2.4f);
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>

1
src/ImageProcessor/Common/Helpers/PixelOperations.cs

@ -6,6 +6,7 @@
namespace ImageProcessor
{
using System;
using System.Numerics;
/// <summary>
/// Performs per-pixel operations.

2
src/ImageProcessor/project.lock.json.REMOVED.git-id

@ -1 +1 @@
3f05708641eb3ed085d4689aae4a960eb067fd16
eb00c54ee74016c2b70f81963e7e8f83cb2dd54b
Loading…
Cancel
Save