# Hex codes

This topic is 3105 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi

I'm doing a program in actionscript 3 and i need to mix two hex codes to get a single hex code, that is, mix two colors and get a mix of the two, like they do here

http://www.colortools.net/color_combination.html

can anyone help?

##### Share on other sites
What do you need help on?

##### Share on other sites
You need to

1. Covnvert the hex string into an uint
2. Sepeate each channel using bitshifts and & operators
3. Combine each channel by adding them together and dividing by 2
4. Assemble a unit by reversing step 2
5. Convert back to a hex string

You can google each step if your not sure how to do this. (Or ask specifically :) )

##### Share on other sites
Not sure if you know hexidecimal, but how it works is you count 0-9 and then A-F for a single digit.

This way you can represent 0-15 with a single digit (F = 15). It does it this way because with 2 digits you can represent 0-255, which is 1 byte.

So with hex you can represent 1 byte with 2 digits.

When you have a color code that looks like this:

#82b892

You are really looking at a number that is made up of 3 bytes.

One byte is for red, one byte is for green, one byte is for blue.

In the above number:
Red = 82
Green = b8
Blue = 92

Those are in hex though, so to convert it to decimal to make it easier to understand:

Red = 110
Green = 184
Blue = 146

Since you have one byte per color channel, those are all out of 255 total.

IE red = 110 is just a little under half intensity red (128 is half, 255 is full).

Anyhow, when you have your two hex numbers broken up into their decimal numbers for each channel, you combine them by averaging them.

If you wanted to combine the above example's color with white (FFFFFF ie 255,255,255) you would do it like this...

Red = (110 + 255) / 2;
Green = (184 + 255) / 2;
Blue = (146 + 255) / 2;

Which is...

Red = 182
Green = 219
Blue = 200

All of the numbers are a bit higher, which means that the white mixed into them and made them brighter (which is what white does!)

When you are done, you need to convert the channels back into hex to get...

Red = B6
Green = DB
Blue = C8

Then you can put it back together into one string to get...

#B6DBC8

so...

#82b892 combined with #ffffff = #b6dbc8

note in your calculations you may get a slightly different result due to rounding but it will be really close :P

and btw, using capital or lower case letters in hex doesn't matter, A means a and B means b, its all the same.

hope this helps!

##### Share on other sites
Here is an example that should help you:
#include <iostream>#include <cmath>using namespace std;int redComp(int color){ 	return (color >> 16) & 0xff; }int greenComp(int color){ 	return (color >> 8) & 0xff; }int blueComp(int color){ 	return (color & 0xff); }int makeColor(int r, int g, int b){	return (r << 16) | (g << 8) | b;}int combine(int color1, int color2){		int r = ceil(( redComp(color1) + redComp(color2) ) / 2.0);	int g = ceil(( greenComp(color1) + greenComp(color2) ) / 2.0);	int b = ceil(( blueComp(color1) + blueComp(color2) ) / 2.0);	return makeColor(r,g,b);}int main(){		int red = 0xabcdef;	int green = 0x123456;	int colorCombined = combine(red,green);	cout << hex << red << " + " << green << " = " << colorCombined << endl;}

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 21
• 15
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634097
• Total Posts
3015514
×