https://blog.csdn.net/A2009374138/article/details/52174856
hsv和rgb
相似度计算
https://blog.csdn.net/hai29785/article/details/55548798
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//public class Hsv
//{
// public float H;
// public float S;
// public float V;
// public Hsv()
// {
// }
// //self-defined
// public Hsv(float h, float s, float v)
// {
// H = h;
// S = s;
// V = v;
// }
// public static double distanceOf(Hsv hsv1, Hsv hsv2)
// {
// float R = 100.0f;
// float angle = 30.0f;
// double h = R * Mathf.Cos(angle / 180 * Mathf.PI);
// double r = R * Mathf.Sin(angle / 180 * Mathf.PI);
// double x1 = r * hsv1.V * hsv1.S * Mathf.Cos(hsv1.H / 180 * Mathf.PI);
// double y1 = r * hsv1.V * hsv1.S * Mathf.Sin(hsv1.H / 180 * Mathf.PI);
// double z1 = h * (1 - hsv1.V);
// double x2 = r * hsv2.V * hsv2.S * Mathf.Cos(hsv2.H / 180 * Mathf.PI);
// double y2 = r * hsv2.V * hsv2.S * Mathf.Sin(hsv2.H / 180 * Mathf.PI);
// double z2 = h * (1 - hsv2.V);
// double dx = x1 - x2;
// double dy = y1 - y2;
// double dz = z1 - z2;
// return Mathf.Sqrt((float)(dx * dx + dy * dy + dz * dz));
// }
// public static double distance1(Hsv hsv1, Hsv hsv2)
// {
// return 0.0f;
// }
//}
/// <summary>
/// HSV颜色坐标
/// </summary>
public class HSV
{
public double H;
public double S;
public double V;
public HSV(int red, int green, int blue)
{
ColorUtil.RGB2HSV(red, green, blue, out H, out S, out V);
}
}
public static class ColorUtil
{
//self-defined
private static double R = 100;
private static double angle = 30;
private static double h = R * Mathf.Cos((float)angle / 180 * Mathf.PI);
private static double r = R * Mathf.Sin((float)angle / 180 * Mathf.PI);
/// <summary>
/// 返回两个颜色在HSV颜色空间的距离
/// </summary>
/// <param name="hsv1"></param>
/// <param name="hsv2"></param>
/// <returns></returns>
public static double DistanceOf(HSV hsv1, HSV hsv2)
{
double x1 = r * hsv1.V * hsv1.S * Mathf.Cos((float)hsv1.H / 180 * Mathf.PI);
double y1 = r * hsv1.V * hsv1.S * Mathf.Sin((float)hsv1.H / 180 * Mathf.PI);
double z1 = h * (1 - hsv1.V);
double x2 = r * hsv2.V * hsv2.S * Mathf.Cos((float)hsv2.H / 180 * Mathf.PI);
double y2 = r * hsv2.V * hsv2.S * Mathf.Sin((float)hsv2.H / 180 * Mathf.PI);
double z2 = h * (1 - hsv2.V);
double dx = x1 - x2;
double dy = y1 - y2;
double dz = z1 - z2;
return Mathf.Sqrt((float)(dx * dx + dy * dy + dz * dz));
}
/// <summary>
/// RGB转换HSV
/// </summary>
/// <param name="red"></param>
/// <param name="green"></param>
/// <param name="blue"></param>
/// <param name="hue"></param>
/// <param name="sat"></param>
/// <param name="bri"></param>
public static void RGB2HSV(int red, int green, int blue, out double hue, out double sat, out double bri)
{
double r = ((double)red / 255.0);
double g = ((double)green / 255.0);
double b = ((double)blue / 255.0);
double max = Mathf.Max((float)r, Mathf.Max((float)g, (float)b));
double min = Mathf.Min((float)r, Mathf.Min((float)g, (float)b));
hue = 0.0;
if (max == r && g >= b)
{
if (max - min == 0) hue = 0.0;
else hue = 60 * (g - b) / (max - min);
}
else if (max == r && g < b)
{
hue = 60 * (g - b) / (max - min) + 360;
}
else if (max == g)
{
hue = 60 * (b - r) / (max - min) + 120;
}
else if (max == b)
{
hue = 60 * (r - g) / (max - min) + 240;
}
sat = (max == 0) ? 0.0 : (1.0 - ((double)min / (double)max));
bri = max;
}
}
public class testColorDiff : MonoBehaviour {
public Image img1;
public Image img2;
public Text tex;
//public Material imgMat1;
//public Material imgMat2;
// Use this for initialization
void Start () {
//imgMat1
}
// Update is called once per frame
void Update () {
}
public void OnBtnClick()
{
Color col1 = Random.ColorHSV();
Color col2 = Random.ColorHSV();
img1.color = col1;
img2.color = col2;
//float h1, s1, v1;
//float h2, s2, v2;
//Color.RGBToHSV(col1, out h1, out s1, out v1);
//Color.RGBToHSV(col2, out h2, out s2, out v2);
//Hsv hsv1 = new Hsv(h1, s1, v1);
//Hsv hsv2 = new Hsv(h2, s2, v2);
//double diff = Hsv.distanceOf(hsv1, hsv2);
HSV hsv1 = new HSV((int)(col1.r * 255), (int)(col1.g * 255), (int)(col1.b * 255));
HSV hsv2 = new HSV((int)(col2.r * 255), (int)(col2.g * 255), (int)(col2.b * 255));
double distance = ColorUtil.DistanceOf(hsv1, hsv2);
tex.text = distance.ToString();
}
public void OnBtn1Click()
{
Camera.main.cullingMask = 0;
}
}