Calculating the average color between two colors in PHP, using an index number as reference value

Posted by Roel Krottje on Stack Overflow See other posts from Stack Overflow or by Roel Krottje
Published on 2010-12-24T11:32:01Z Indexed on 2010/12/24 11:54 UTC
Read the original article Hit count: 447

Filed under:
|
|

Hi all!

In PHP, I am trying to calculate the average color (in hex) between to different hex colors.. However, I also need to be able to supply an index number between 0.0 and 1.0.

So for example:

I have $color1 = "#ffffff" and $color2 = "#0066CC" ..

If I would write a function to get the average color and I would supply 0.0 as the index number, the function would need to return "#ffffff". If I would supply 1.0 as the index number, the function would need to return "#0066CC".. However if I would supply 0.2, the function would need to return an average color between the two colors, but still closer to color1 than to color2.. If I would supply index number 0.5, I would get the exact average color of both colors..

I have been trying to accomplish this for several days now but I can't seem to figure it out..! Any help would therefor be greatly appreciated!..

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about colors