Stephane
Stephane

Reputation: 5078

Calculating color combination with hex values

In additive color mixing primary colors are Red, Green and Blue (RGB).

Red    = #ff0000
Green  = #00ff00
Blue   = #0000ff

Combining Red (#ff0000) and Green (#00ff00) makes Yellow (#ffff00)

Is there some formula to calulate the hex code of a color resulting from the combination of two others ?

Something like #ff0000 + #00ff00 when applied to such a formula gives #ffff00

Upvotes: 1

Views: 7660

Answers (1)

Ayush
Ayush

Reputation: 42440

You can add two HEX string like this in PHP:

$red = "FF0000";
$green = "00FF00";

$yellow = dechex(hexdec($red) + hexdec($green));

echo $yellow;

Live Demo

What that snippet is basically doing is converting the hex strings to numbers, adding them together, and then converting the sum back to a hex string.

Reference Links:

hexdec | dechex

Upvotes: 7

Related Questions