One common way for software specifications such as HTML to specify colors is with a hexadecimal string. For instance the color aquamarine is represented by the string"#7FFFD4"
. Here's how the string breaks down:
- The first character is always
"#"
.
- The second and third character are the red channel value, represented as a hexadecimal value between
00
andFF
. In this example, the red channel value is 127, which in hexadecimal is7F
.
- The fourth and fifth character are the green channel value, represented the same way. In this example, the green channel value is 255, which in hexadecimal is
FF
.
- The sixth and seventh character are the blue channel value, represented the same way. In this example, the blue channel value is 212, which in hexadecimal is
D4
.
All three channel values must be an integer between 0 (minimum brightness) and 255 (maximum brightness). In all cases the hex values are two digits each, including a leading0
if necessary.See the Wikipedia page for more examples, and a link for how to convert a number to hexadecimal.
Assignment
Given three integers between 0 and 255, corresponding to the red, green, and blue channel values of a color, find the hex string for that color. You may use anything built into your programming language, such as for base conversion, but you can also do it manually.
Example
R: 255
G: 99
B: 71
HEX: #FF6347
Hint
You can use built in C# functionality to achieve this. If you declare the 0-255 value as a byte, you can then useToString()on it, in conjunction with specialformat stringinside the parenthesis (look for "X" format string in the table on the linked page).
Extra Credit
Given two hex color strings, produce the hex color string you get from averaging their RGB values together. You'll need to round channel values to integers.
HEX Color 1: #000000
HEX Color 2: #778899
HEX Blended: #3C444C
This is not actually the best way to blend two hex colors: to do it properly you need gamma correction. But that's another story.