开发者

How to find a "sufficiently different" RGB?

开发者 https://www.devze.com 2023-04-01 12:27 出处:网络
Provided you are given a color in a form of RGB, is there a way to programmatically determine whether it is sufficiencly different from some other RGB.

Provided you are given a color in a form of RGB, is there a way to programmatically determine whether it is sufficiencly different from some other RGB.

Say, i'd like to test whe开发者_如何学JAVAther colors are at least 30% apart from one another, how do i do it? Or, put differently, how can i generate a color that is sufficiently different from another color?


this is a minefield, because human optical processing is horribly messy. but what you're asking about is colour difference - that page gives various different formulae that can be used to calculate different values that represent the "difference" between two colours.

[update] and here's an online calculator that will apparently calculate those differences for you (it's complicated and links don't change urls, so it's hard to link to, but if you click around it might make sense....)


This is very very subjective. However, I personally think your best chance is to use hue-saturation-lightness (HSL, or HSV for that matter), and try to get a value out of the difference in the values. For example something like:

value = a*abs(H1-H2)+b*abs(S1-S2)+c*abs(L1-L2);

and try to, with trial and error, find the best a, b, and c constants that differentiate the colors the same amount as your human judgment does. If you know linear regression, you could have some sample colors with the differences you assign and get the a, b and c values with linear regression.

A more appropriate formula may contain differences in power of 2 instead of abs for example and more importantly values for a, b and c as functions of the colors themselves rather than constants.


I think it's the same problem you had with colors, some hours ago, in a different question. Read about HSV (or HSL) in wikipediahttp://en.wikipedia.org/wiki/HSL_and_HSV: when you transform RGB -> HSV, the H value is the color, S and V are other characteristics.

If you want to know the distance between colors, just measure the H value. If you transform it to a 360 degrees notation, 30 degrees is enough to give you very different colors.

If you want to know it in RGB colors, it gets a bit harder: the values R, G and B can lead you to a grayish "color", not exactly a color.


Treat the RGB value as a point and use the distance formula from geometry.

distance = ( (R2-R1)^2 + (G2-G1)^2 + (B2-B1)^2 )^.5
distance > minimun value

In the case of wanting 30% difference, make minimum value = the distance of point_1 * .3 from the origin, ie., .3( (R1)^2 + (G1)^2 + (B1)^2 )^.5

0

精彩评论

暂无评论...
验证码 换一张
取 消