From edcb0664b13ac2ab2024929389853b530d8e04a2 Mon Sep 17 00:00:00 2001 From: Fraser Waters Date: Mon, 10 Nov 2014 02:42:12 +0100 Subject: [PATCH] Add To/From YUV methods to Color4. --- Source/OpenTK/Graphics/Color4.cs | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Source/OpenTK/Graphics/Color4.cs b/Source/OpenTK/Graphics/Color4.cs index 3b136f0..265de06 100644 --- a/Source/OpenTK/Graphics/Color4.cs +++ b/Source/OpenTK/Graphics/Color4.cs @@ -1276,6 +1276,47 @@ namespace OpenTK.Graphics return new Vector4(x, y, z, rgb.A); } + #endregion + + #region YUV + + /// + /// Converts YCbCr color values to RGB color values. + /// + /// + /// Returns the converted color value. + /// + /// + /// Color value to convert in Luma-Chrominance (YCbCr) aka YUV. + /// The X element contains Luma (Y, 0.0 to 1.0), the Y element contains Blue-difference chroma (U, -0.5 to 0.5), the Z element contains the Red-difference chroma (V, -0.5 to 0.5), and the W element contains the Alpha (which is copied to the output's Alpha value). + /// + /// Converts using ITU-R BT.601/CCIR 601 W(r) = 0.299 W(b) = 0.114 U(max) = 0.436 V(max) = 0.615. + public static Color4 FromYcbcr(Vector4 ycbcr) + { + var r = 1.0f * ycbcr.X + 0.0f * ycbcr.Y + 1.402f * ycbcr.Z; + var g = 1.0f * ycbcr.X + -0.344136f * ycbcr.Y + -0.714136f * ycbcr.Z; + var b = 1.0f * ycbcr.X + 1.772f * ycbcr.Y + 0.0f * ycbcr.Z; + return new Color4(r, g, b, ycbcr.W); + } + + /// + /// Converts RGB color values to YUV color values. + /// + /// + /// Returns the converted color value in Luma-Chrominance (YCbCr) aka YUV. + /// The X element contains Luma (Y, 0.0 to 1.0), the Y element contains Blue-difference chroma (U, -0.5 to 0.5), the Z element contains the Red-difference chroma (V, -0.5 to 0.5), and the W element contains the Alpha (a copy of the input's Alpha value). + /// Each has a range of 0.0 to 1.0. + /// + /// Color value to convert. + /// Converts using ITU-R BT.601/CCIR 601 W(r) = 0.299 W(b) = 0.114 U(max) = 0.436 V(max) = 0.615. + public static Vector4 ToYcbcr(Color4 rgb) + { + var y = 0.299f * rgb.R + 0.587f * rgb.G + 0.114f * rgb.B; + var u = -0.168736f * rgb.R + -0.331264f * rgb.G + 0.5f * rgb.B; + var v = 0.5f * rgb.R + -0.418688f * rgb.G + -0.081312f * rgb.B; + return new Vector4(y, u, v, rgb.A); + } + #endregion #endregion -- 2.7.4