CPF/CPF.ReoGrid/Utility/ColorUtility.cs

222 lines
4.7 KiB
C#
Raw Normal View History

2024-06-24 10:15:59 +08:00
using System;
using System.Diagnostics;
using CPF.Drawing;
namespace CPF.ReoGrid.Utility
{
internal static class ColorUtility
{
public static HSLColor RGBToHSL(Color rgbColor)
{
HSLColor hslcolor = default(HSLColor);
float num = (float)rgbColor.R / 255f;
float num2 = (float)rgbColor.G / 255f;
float num3 = (float)rgbColor.B / 255f;
float a = (float)rgbColor.A / 255f;
float num4 = Math.Min(num, Math.Min(num2, num3));
float num5 = Math.Max(num, Math.Max(num2, num3));
float num6 = num5 - num4;
hslcolor.A = a;
bool flag = num5 == num4;
HSLColor result;
if (flag)
{
hslcolor.H = 0f;
hslcolor.S = 0f;
hslcolor.L = num5;
result = hslcolor;
}
else
{
hslcolor.L = (num4 + num5) / 2f;
bool flag2 = hslcolor.L < 0.5f;
if (flag2)
{
hslcolor.S = num6 / (num5 + num4);
}
else
{
hslcolor.S = num6 / (2f - num5 - num4);
}
bool flag3 = num == num5;
if (flag3)
{
hslcolor.H = (num2 - num3) / num6;
}
bool flag4 = num2 == num5;
if (flag4)
{
hslcolor.H = 2f + (num3 - num) / num6;
}
bool flag5 = num3 == num5;
if (flag5)
{
hslcolor.H = 4f + (num - num2) / num6;
}
hslcolor.H *= 60f;
bool flag6 = hslcolor.H < 0f;
if (flag6)
{
hslcolor.H += 360f;
}
result = hslcolor;
}
return result;
}
public static Color HSLToRgb(HSLColor hslColor)
{
Color color = default(Color);
bool flag = hslColor.S == 0f;
Color result;
if (flag)
{
color = Color.FromArgb((byte)(hslColor.A * 255f), (byte)(hslColor.L * 255f), (byte)(hslColor.L * 255f), (byte)(hslColor.L * 255f));
result = color;
}
else
{
bool flag2 = hslColor.L < 0.5f;
float num;
if (flag2)
{
num = hslColor.L * (1f + hslColor.S);
}
else
{
num = hslColor.L + hslColor.S - hslColor.L * hslColor.S;
}
float t = 2f * hslColor.L - num;
float num2 = hslColor.H / 360f;
float t2 = num2 + 0.33333334f;
float num3 = ColorUtility.SetColor(num, t, t2);
float t3 = num2;
float num4 = ColorUtility.SetColor(num, t, t3);
float t4 = num2 - 0.33333334f;
float num5 = ColorUtility.SetColor(num, t, t4);
color = Color.FromArgb((byte)(hslColor.A * 255f), (byte)(num3 * 255f), (byte)(num4 * 255f), (byte)(num5 * 255f));
result = color;
}
return result;
}
private static float SetColor(float t1, float t2, float t3)
{
bool flag = t3 < 0f;
if (flag)
{
t3 += 1f;
}
bool flag2 = t3 > 1f;
if (flag2)
{
t3 -= 1f;
}
bool flag3 = 6f * t3 < 1f;
float result;
if (flag3)
{
result = t2 + (t1 - t2) * 6f * t3;
}
else
{
bool flag4 = 2f * t3 < 1f;
if (flag4)
{
result = t1;
}
else
{
bool flag5 = 3f * t3 < 2f;
if (flag5)
{
result = t2 + (t1 - t2) * (0.6666667f - t3) * 6f;
}
else
{
result = t2;
}
}
}
return result;
}
public static float CalculateFinalLumValue(float tint, float lum)
{
bool flag = tint < 0f;
float result;
if (flag)
{
result = lum * (1f + tint);
}
else
{
result = lum * (1f - tint) + (255f - 255f * (1f - tint));
}
return result;
}
public static Color LightColor(Color color)
{
return ColorUtility.ChangeColorBrightness(color, 0.1f);
}
public static Color LightLightColor(Color color)
{
return ColorUtility.ChangeColorBrightness(color, 0.2f);
}
public static Color LightLightLightColor(Color color)
{
return ColorUtility.ChangeColorBrightness(color, 0.3f);
}
public static Color DarkColor(Color color)
{
return ColorUtility.ChangeColorBrightness(color, -0.1f);
}
public static Color DarkDarkColor(Color color)
{
return ColorUtility.ChangeColorBrightness(color, -0.2f);
}
public static Color DarkDarkDarkColor(Color color)
{
return ColorUtility.ChangeColorBrightness(color, -0.3f);
}
public static Color ChangeColorBrightness(Color color, float brightnessFactor)
{
HSLColor hslcolor = ColorUtility.RGBToHSL(color);
hslcolor.L += brightnessFactor;
bool flag = hslcolor.L > 1f;
if (flag)
{
hslcolor.L = 1f;
}
else
{
bool flag2 = hslcolor.L < 0f;
if (flag2)
{
hslcolor.L = 0f;
}
}
return ColorUtility.HSLToRgb(hslcolor);
}
public static Color FromAlphaColor(byte alpha, Color color)
{
return Color.FromArgb(alpha, color.R, color.G, color.B);
}
public static Color FromARGBValue(long value)
{
Color result = Color.FromArgb((byte)(value >> 24 & 255L), (byte)(value >> 16 & 255L), (byte)(value >> 8 & 255L), (byte)(value & 255L));
Debug.Assert(result.A > 0);
return result;
}
}
}