2016-06-19 2 views
1

당신이 내 색상 C# 컬러

일치를 도와 줄 수 일치 나는 그것이 작동하지 않았다 몇 가지 코드를 검색하려고 아니라

내 논리가 5 % 또는 10 % 가까운 같은 조정 허용 오차, 아래처럼 색상 :

Red and Light Red = True  
Red and Dark Red = True 
Red and black = False 

여기 내 코드는하지만 어쩌면

public static bool MatchArgb(int Argb1, int Argb2, int tolerance) 
{ 
    Color c1 = Color.FromArgb(Argb1); 
    Color c2 = Color.FromArgb(Argb2); 

    return Math.Abs(c1.R - c2.R) <= tolerance^
      Math.Abs(c1.G - c2.G) <= tolerance^
      Math.Abs(c1.B - c2.B) <= tolerance; 
} 

public static bool MatchColor(Color c1, Color c2, int tolerance) 
{ 
    return Math.Abs(c1.R - c2.R) <= tolerance^
      Math.Abs(c1.G - c2.G) <= tolerance^
      Math.Abs(c1.B - c2.B) <= tolerance; 
} 

답변

2

그것은 내가 아주 잘 작동하지 않았다 이것이 Paint.NET에서 어떻게 이루어 졌는지 확인하는 것이 좋습니다. 나는 그것의 클론과 그에 상응하는 소스 코드를 발견했다 : Pinta/Flood Tool

private static bool CheckColor (ColorBgra a, ColorBgra b, int tolerance) 
    { 
     int sum = 0; 
     int diff; 

     diff = a.R - b.R; 
     sum += (1 + diff * diff) * a.A/256; 

     diff = a.G - b.G; 
     sum += (1 + diff * diff) * a.A/256; 

     diff = a.B - b.B; 
     sum += (1 + diff * diff) * a.A/256; 

     diff = a.A - b.A; 
     sum += diff * diff; 

     return (sum <= tolerance * tolerance * 4); 
    } 
+0

감사합니다. 30 공차에 :) – ayanix