How to use rgb difference between two colors in C# in order to find the closest match? -
i have code gets closest color based on rgb difference, dark colors dark blue, returns black instead of "dark blue". can u guys me finding out what's happening?
this code calculates rgb difference between 2 colors:
int colordiff(color c1, color c2) { return (int)math.sqrt((c1.r - c2.r) * (c1.r - c2.r) + (c1.g - c2.g) * (c1.g - c2.g) + (c1.b - c2.b) * (c1.b - c2.b)); }
this code gets closest color on list (the 1 has less difference between rgb):
int encontrarcor(list<color> colors, color target) { var colordiffs = colors.select(n => colordiff(n, target)).min(n => n); return colors.findindex(n => colordiff(n, target) == colordiffs); }
what think happening that, can see in first code, does: (c1.r - c2.r) * (c1.r - c2.r), if 1 of these substrates equal 0, whole product gonna 0, returns black, cause rgb code of black 0,0,0.
i've tried make myself clear possible, sorry if little bit confused.
i don't point of square root , multiplications, differences should positive:
int rgbdiff(color c1, color c2) { return math.abs(c1.r - c2.r) + math.abs(c1.g - c2.g) + math.abs(c1.b - c2.b); } color closestcolor(color target, ienumerable<color> colors) { return colors.min(c => tuple.create(rgbdiff(c, targert), c)).item2; }
note more 1 color can have same difference.
Comments
Post a Comment