From c734bfddccd7f28217b691397d3186e0aa9e2fd3 Mon Sep 17 00:00:00 2001 From: James South Date: Tue, 10 Nov 2015 23:53:32 +1100 Subject: [PATCH] Add Compand methods to Color This will eventually replace the PixelOperations methods. Former-commit-id: a1058efb148c3fee54c69000698c96ade0c99c4f Former-commit-id: 3704335fe924758d8d615ddc377f06b061308f0b Former-commit-id: cb03cf38dab552cf2677fcfd8396f19906b80f1d --- src/ImageProcessor/Colors/Color.cs | 72 +++++++++++++++++++ .../Common/Helpers/PixelOperations.cs | 1 + .../project.lock.json.REMOVED.git-id | 2 +- 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/ImageProcessor/Colors/Color.cs b/src/ImageProcessor/Colors/Color.cs index d85464471..62d4aef7b 100644 --- a/src/ImageProcessor/Colors/Color.cs +++ b/src/ImageProcessor/Colors/Color.cs @@ -473,6 +473,40 @@ namespace ImageProcessor return (from * (1 - amount)) + (to * amount); } + /// + /// Compresseses a linear color signal to its sRGB equivalent. + /// + /// + /// + /// The whos signal to compress. + /// The . + 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); + } + + /// + /// Expands an sRGB color signal to its linear equivalent. + /// + /// + /// + /// The whos signal to expand. + /// The . + 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); + } + /// /// Gets a representation for this . /// @@ -527,6 +561,44 @@ namespace ImageProcessor return this.backingVector.Equals(other.backingVector); } + /// + /// Gets the compressed sRGB value from an linear signal. + /// + /// + /// + /// The signal value to compress. + /// + /// The . + /// + 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; + } + + /// + /// Gets the expanded linear value from an sRGB signal. + /// + /// + /// + /// The signal value to expand. + /// + /// The . + /// + 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); + } + /// /// Returns the hash code for this instance. /// diff --git a/src/ImageProcessor/Common/Helpers/PixelOperations.cs b/src/ImageProcessor/Common/Helpers/PixelOperations.cs index a116c0e35..40a0fb399 100644 --- a/src/ImageProcessor/Common/Helpers/PixelOperations.cs +++ b/src/ImageProcessor/Common/Helpers/PixelOperations.cs @@ -6,6 +6,7 @@ namespace ImageProcessor { using System; + using System.Numerics; /// /// Performs per-pixel operations. diff --git a/src/ImageProcessor/project.lock.json.REMOVED.git-id b/src/ImageProcessor/project.lock.json.REMOVED.git-id index 24339fed2..dba2656f5 100644 --- a/src/ImageProcessor/project.lock.json.REMOVED.git-id +++ b/src/ImageProcessor/project.lock.json.REMOVED.git-id @@ -1 +1 @@ -3f05708641eb3ed085d4689aae4a960eb067fd16 \ No newline at end of file +eb00c54ee74016c2b70f81963e7e8f83cb2dd54b \ No newline at end of file