Files
Simulator/DroneClient/utils/UtilityUI.cs

75 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DroneClient.utils
{
static class UtilityUI
{
public static Color ColorFromHsb(float hue, float saturation, float brightness)
{
// Ограничиваем значения параметров
hue = Math.Clamp(hue, 0, 360);
saturation = Math.Clamp(saturation, 0, 1);
brightness = Math.Clamp(brightness, 0, 1);
float c = brightness * saturation;
float x = c * (1 - Math.Abs((hue / 60) % 2 - 1));
float m = brightness - c;
float r, g, b;
if (hue < 60) { r = c; g = x; b = 0; }
else if (hue < 120) { r = x; g = c; b = 0; }
else if (hue < 180) { r = 0; g = c; b = x; }
else if (hue < 240) { r = 0; g = x; b = c; }
else if (hue < 300) { r = x; g = 0; b = c; }
else { r = c; g = 0; b = x; }
return Color.FromArgb(
(int)((r + m) * 255),
(int)((g + m) * 255),
(int)((b + m) * 255));
}
public static void ColorToHsb(Color color, out float hue, out float saturation, out float brightness)
{
float r = color.R / 255f;
float g = color.G / 255f;
float b = color.B / 255f;
float max = Math.Max(r, Math.Max(g, b));
float min = Math.Min(r, Math.Min(g, b));
float delta = max - min;
// Hue calculation
if (delta == 0)
{
hue = 0;
}
else if (max == r)
{
hue = 60 * (((g - b) / delta) % 6);
}
else if (max == g)
{
hue = 60 * (((b - r) / delta) + 2);
}
else
{
hue = 60 * (((r - g) / delta) + 4);
}
if (hue < 0) hue += 360;
// Saturation calculation
saturation = max == 0 ? 0 : delta / max;
// Brightness/Value
brightness = max;
}
}
}