From 5bb277d9ecb845211646db3b08a3d6e32c6800d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Wed, 17 Aug 2022 18:03:41 +0200 Subject: [PATCH] LibGfx: Add YUV conversion functions to Color --- Userland/Libraries/LibGfx/Color.h | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Userland/Libraries/LibGfx/Color.h b/Userland/Libraries/LibGfx/Color.h index 536a0f7aec..4277089712 100644 --- a/Userland/Libraries/LibGfx/Color.h +++ b/Userland/Libraries/LibGfx/Color.h @@ -25,6 +25,12 @@ struct HSV { double value { 0 }; }; +struct YUV { + float y { 0 }; + float u { 0 }; + float v { 0 }; +}; + class Color { public: enum NamedColor { @@ -75,6 +81,37 @@ public: return Color(r, g, b); } + static constexpr Color from_yuv(YUV const& yuv) { return from_yuv(yuv.y, yuv.u, yuv.v); } + static constexpr Color from_yuv(float y, float u, float v) + { + // https://www.itu.int/rec/R-REC-BT.1700-0-200502-I/en Table 4, Items 8 and 9 arithmetically inverted + float r = y + v / 0.877f; + float b = y + u / 0.493f; + float g = (y - 0.299f * r - 0.114f * b) / 0.587f; + r = clamp(r, 0.0f, 1.0f); + g = clamp(g, 0.0f, 1.0f); + b = clamp(b, 0.0f, 1.0f); + + return { static_cast(floorf(r * 255.0f)), static_cast(floorf(g * 255.0f)), static_cast(floorf(b * 255.0f)) }; + } + + // https://www.itu.int/rec/R-REC-BT.1700-0-200502-I/en Table 4 + constexpr YUV to_yuv() const + { + float r = red() / 255.0f; + float g = green() / 255.0f; + float b = blue() / 255.0f; + // Item 8 + float y = 0.299f * r + 0.587f * g + 0.114f * b; + // Item 9 + float u = 0.493f * (b - y); + float v = 0.877f * (r - y); + y = clamp(y, 0.0f, 1.0f); + u = clamp(u, -1.0f, 1.0f); + v = clamp(v, -1.0f, 1.0f); + return { y, u, v }; + } + static constexpr Color from_hsl(float h_degrees, float s, float l) { return from_hsla(h_degrees, s, l, 1.0); } static constexpr Color from_hsla(float h_degrees, float s, float l, float a) {