diff --git a/src/devices/Media/VideoDevice/VideoDevice.Converter.cs b/src/devices/Media/VideoDevice/VideoDevice.Converter.cs index 6712dc35fb46fd40f3cbc858868b4527e013783c..5f9d82faac7441263b811eb60024bbf8e257c1bd 100644 --- a/src/devices/Media/VideoDevice/VideoDevice.Converter.cs +++ b/src/devices/Media/VideoDevice/VideoDevice.Converter.cs @@ -159,11 +159,13 @@ namespace Iot.Device.Media /// RGB color. private static Color YuvToRgb(int y, int u, int v) { - byte r = (byte)(y + 1.4075 * (v - 128)); - byte g = (byte)(y - 0.3455 * (u - 128) - (0.7169 * (v - 128))); - byte b = (byte)(y + 1.7790 * (u - 128)); + byte r = Clamp(y + 1.4075 * (v - 128)); + byte g = Clamp(y - 0.3455 * (u - 128) - (0.7169 * (v - 128))); + byte b = Clamp(y + 1.7790 * (u - 128)); return Color.FromArgb(r, g, b); } + + private static byte Clamp(double val) => (byte)(val > 255 ? 255 : val < 0 ? 0 : val); } }