Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find percent value of a color between two known colors

What is the best way to determine the percent value of a color between two given values. In other words, what is the best way to calculate the % position of color C?

Color A - 0x0000FF Color B - 0x00CCFF Color C - 0x00FFFF

Thanks!

like image 803
Andrei Avatar asked Jan 20 '11 03:01

Andrei


People also ask

How do you compare two colors?

The most common method would be a visual color comparison by looking at two physical color samples side by side under a light source. Color is very relative, so you can compare colors in terms of the other color across dimensions such as hue, lightness and saturation (brightness).

How is color difference calculated?

For two samples in Riemannian colour space, the colour difference can be calculated by integrating the quadratic equation for curvature along the geodesic distance between the points and dividing the result by a constant called least perceptible difference (McDonald 1982; Wyszecki and Stiles, 1982).

What is the percentage of RGB?

Functional RGB colors The generic syntax for this type of color value is rgb(color) , where color is expressed using a triplet of either percentages or integers. The percentage values can be in the range 0% – 100% , and the integers can be in the range 0 – 255 .


1 Answers

well it's not that straight forward, because it depends on how you decide to quantize color.

You could do it through HSB, which is the more correct way in my opinion (though not necessarily a fact though) or just use the hex value.

quickest is probably to do it using the hax values

var colour:uint = 0x9900CC;
var r:uint = colour >> 16;
var g:uint = colour >> 8 & 0xFF;
var b:uint = colour & 0xFF;

.this will give you the value of each channel(c) (ABC being the colours)

then do the math for each channel (c)

(cB - cA)/(cC - cA)

then once you get each of these chanels, you can add them together and divide by 3.

there is one problem though, if Colour A and C are ever the same for any channel, you need to add an exception (because cC and CA are zero and you can't divide by zero), at that point you also need to decide how to handle that difference.

like image 192
Daniel Avatar answered Oct 11 '22 16:10

Daniel