2 Star 19 Fork 1.1K

独立观察员 / DotNetCodes

forked from 奎宇工作室 / DotNetCodes 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
DrawingColorHelper.cs 4.12 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
using System;
using System.Drawing;
/*
* 源码己托管: http://gitee.com/dlgcy/dotnetcodes
* 版本:2023年5月13日
*/
namespace DotNet.Utilities
{
/// <summary>
/// 绘图颜色帮助类;
/// </summary>
public class DrawingColorHelper
{
#region 颜色转换
/// <summary>
/// 从 ARGB 字符串转换为绘图颜色
/// https://www.cnblogs.com/lelehellow/p/6369608.html
/// </summary>
/// <param name="colorStr">ARGB颜色字符串(如#FFFFFFFF)</param>
/// <returns>Color 对象,转换失败返回透明色</returns>
public static Color ArgbStrToDrawingColor(string colorStr)
{
if (colorStr.Length != 9 || !colorStr.StartsWith("#"))
{
throw new Exception("颜色字符串格式不正确");
}
try
{
byte[] argb = new byte[4];
for (int i = 0; i < 4; i++)
{
char[] charArray = colorStr.Substring(i * 2 + 1, 2).ToCharArray();
byte b1 = ToByte(charArray[0]);
byte b2 = ToByte(charArray[1]);
argb[i] = (byte)(b2 | (b1 << 4));
}
return Color.FromArgb(argb[0], argb[1], argb[2], argb[3]);
}
catch (Exception ex)
{
Console.WriteLine(ex);
return Color.Transparent;
}
}
private static byte ToByte(char c)
{
byte b = (byte)"0123456789ABCDEF".IndexOf(c);
return b;
}
/// <summary>
/// 从 RGB 字符串转换为绘图颜色
/// </summary>
/// <param name="colorStr">RGB颜色字符串(如#FFFFFF)</param>
/// <returns>Color 对象,转换失败返回透明色</returns>
public static Color RgbStrToDrawingColor(string colorStr)
{
colorStr = colorStr.Replace("#", "#FF");
return ArgbStrToDrawingColor(colorStr);
}
#endregion
#region 相似比较
/// <summary>
/// [ChatGPT] 计算两个颜色之间的欧几里得距离(即两个颜色在 RGB 空间中的距离)
/// </summary>
public static double ColorDistance(Color color1, Color color2)
{
int rDiff = color1.R - color2.R;
int gDiff = color1.G - color2.G;
int bDiff = color1.B - color2.B;
return Math.Sqrt(rDiff * rDiff + gDiff * gDiff + bDiff * bDiff);
}
/// <summary>
/// [ChatGPT] 判断两个颜色是否近似(使用 两个颜色之间的欧几里得距离 与 给定阈值 进行比较,如果距离小于指定的阈值,则认为这两个颜色近似)
/// </summary>
public static bool AreColorsSimilar1(Color color1, Color color2, double threshold = 26)
{
double distance = ColorDistance(color1, color2);
return distance <= threshold;
}
/// <summary>
/// 获取两个颜色的 RGB 分量差之和
/// </summary>
public static int ColorSumOfComponentDifferences(Color color1, Color color2)
{
int rDiff = Math.Abs(color1.R - color2.R);
int gDiff = Math.Abs(color1.G - color2.G);
int bDiff = Math.Abs(color1.B - color2.B);
return rDiff + gDiff + bDiff;
}
/// <summary>
/// [ChatGPT] 判断两个颜色是否近似(判断两个颜色的 RGB 分量差之和是否小于指定的阈值,如果小于则认为这两个颜色近似)
/// </summary>
/// <param name="color1"></param>
/// <param name="color2"></param>
/// <param name="threshold"></param>
/// <returns></returns>
public static bool AreColorsSimilar2(Color color1, Color color2, int threshold = 45)
{
int sum = ColorSumOfComponentDifferences(color1, color2);
return sum <= threshold;
}
#endregion
}
}
//附-WPF的媒体颜色帮助类:https://gitee.com/dlgcy/WPFTemplateLib/blob/master/WpfHelpers/MediaColorHelper.cs
C#
1
https://gitee.com/dlgcy/dotnetcodes.git
git@gitee.com:dlgcy/dotnetcodes.git
dlgcy
dotnetcodes
DotNetCodes
dlgcy

搜索帮助

14c37bed 8189591 565d56ea 8189591