diff --git a/src/ImageProcessorCore/Colors/Color.cs b/src/ImageProcessorCore/Colors/Color.cs index 7b1dde512..c365f3672 100644 --- a/src/ImageProcessorCore/Colors/Color.cs +++ b/src/ImageProcessorCore/Colors/Color.cs @@ -43,7 +43,7 @@ namespace ImageProcessorCore /// The alpha component. public Color(byte r, byte g, byte b, byte a = 255) { - this.packedValue = (uint)(r << 24 | g << 16 | b << 8 | a); + this.packedValue = Pack(r, g, b, a); } /// @@ -219,6 +219,44 @@ namespace ImageProcessorCore return new Color(hex); } + /// + public void PackFromBytes(byte r, byte g, byte b, byte a) + { + this.packedValue = Pack(r, g, b, a); + } + + /// + public void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder) + { + switch (componentOrder) + { + case ComponentOrder.BGR: + bytes[startIndex] = this.B; + bytes[startIndex + 1] = this.G; + bytes[startIndex + 2] = this.R; + break; + case ComponentOrder.BGRA: + bytes[startIndex] = this.B; + bytes[startIndex + 1] = this.G; + bytes[startIndex + 2] = this.R; + bytes[startIndex + 3] = this.A; + break; + case ComponentOrder.RGB: + bytes[startIndex] = this.R; + bytes[startIndex + 1] = this.G; + bytes[startIndex + 2] = this.B; + break; + case ComponentOrder.RGBA: + bytes[startIndex] = this.R; + bytes[startIndex + 1] = this.G; + bytes[startIndex + 2] = this.B; + bytes[startIndex + 3] = this.A; + break; + default: + throw new NotSupportedException(); + } + } + /// /// Converts the value of this instance to a hexadecimal string. /// @@ -295,7 +333,7 @@ namespace ImageProcessorCore } /// - /// Packs the four floats into a uint. + /// Packs the four floats into a . /// /// The x-component /// The y-component @@ -308,6 +346,19 @@ namespace ImageProcessorCore return Pack(ref value); } + /// + /// Packs the four floats into a . + /// + /// The x-component + /// The y-component + /// The z-component + /// The w-component + /// The + private static uint Pack(byte x, byte y, byte z, byte w) + { + return (uint)(x << 24 | y << 16 | z << 8 | w); + } + /// /// Converts the specified hex value to an rrggbbaa hex value. /// diff --git a/src/ImageProcessorCore/Colors/ComponentOrder.cs b/src/ImageProcessorCore/Colors/ComponentOrder.cs new file mode 100644 index 000000000..0bf02b8a5 --- /dev/null +++ b/src/ImageProcessorCore/Colors/ComponentOrder.cs @@ -0,0 +1,33 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageProcessorCore +{ + /// + /// Enumerates the various component orders. + /// + public enum ComponentOrder + { + /// + /// Blue-> Green-> Red order. + /// + BGR, + + /// + /// Blue-> Green-> Red-> Alpha order. + /// + BGRA, + + /// + /// Red-> Green-> Blue order. + /// + RGB, + + /// + /// Red-> Green-> Blue-> Alpha order. + /// + RGBA, + } +} diff --git a/src/ImageProcessorCore/Colors/PackedPixel/IPackedBytes.cs b/src/ImageProcessorCore/Colors/PackedPixel/IPackedBytes.cs new file mode 100644 index 000000000..6d043308a --- /dev/null +++ b/src/ImageProcessorCore/Colors/PackedPixel/IPackedBytes.cs @@ -0,0 +1,31 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageProcessorCore +{ + /// + /// An interface that converts packed vector types to and from values, + /// allowing multiple encodings to be manipulated in a generic manner. + /// + public interface IPackedBytes + { + /// + /// Gets the packed representation from the gives bytes. + /// + /// The x-component. + /// The y-component. + /// The z-omponent. + /// The w-component. + void PackFromBytes(byte x, byte y, byte z, byte w); + + /// + /// Sets the packed representation into the gives bytes. + /// + /// The bytes to set the color in. + /// The starting index of the . + /// The order of the components. + void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder); + } +} diff --git a/src/ImageProcessorCore/Colors/PackedPixel/IPackedPixel.cs b/src/ImageProcessorCore/Colors/PackedPixel/IPackedPixel.cs index 6a54855ca..5bf2dc3ea 100644 --- a/src/ImageProcessorCore/Colors/PackedPixel/IPackedPixel.cs +++ b/src/ImageProcessorCore/Colors/PackedPixel/IPackedPixel.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // @@ -9,7 +9,7 @@ namespace ImageProcessorCore /// An interface that represents a packed pixel type. /// /// The packed format. uint, long, float. - public interface IPackedPixel : IPackedVector + public interface IPackedPixel : IPackedVector, IPackedBytes where TPacked : struct { } diff --git a/src/ImageProcessorCore/Colors/PackedVector/IPackedVector.cs b/src/ImageProcessorCore/Colors/PackedPixel/IPackedVector.cs similarity index 100% rename from src/ImageProcessorCore/Colors/PackedVector/IPackedVector.cs rename to src/ImageProcessorCore/Colors/PackedPixel/IPackedVector.cs