It has been established that greys are an equal mix of red, green, and blue, and for this method all three values are added together then averaged out. Having extracted the RGB value – in this case a red of rgb(255,0,0) – the next step is to convert it to a grey. Two methods are presented here, although the second is essentially a tweaked improvement of the first. With the necessary colour, CSS, and JavaScript concepts introduced, it’s time to focus on the conversion algorithms. The console.log(lor) returns the colour value – originally specified in hexadecimal as #FF0000– logging the following line in the developer console: getComputedStyle ( alert, null ) console. getElementById ( ' alert ' ) var style = window.
To retrieve the colour of the “warning!!!” text – in this case, a red – one would make use of the the following JavaScript code: var alert = document. To see behaviour this in action, consider the following HTML: These are decimal values, as opposed to hexadecimal or percentile. When extracting colour values from HTML elements using JavaScript, the browser returns RGB values using another form of CSS notation: rgb(0-255,0-255,0-255). Using just two digits, hexadecimal can therefore represent a maximum value of 255, whereas decimal tops-out at 99. Below is a comparison: the first line counts up using decimal the line below it’s the equivalent in hexadecimal:Ġ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18. To make up those extra digits (fingers?), hexadecimal adds a few letters. This system works nicely for counting on ten fingers – but using base 16 (hexadecimal) is like counting on sixteen fingers. To explain this as simply as possible: humans use a base 10 (decimal) counting system.
We can therefore surmise that 100% is equal to FF, which is actually equal to 255 … but, why not 100, or 99? Notice that instead of 100% for red, there’s an FF and that the 0%’s are now padded to 00 in order to fill two places. Or more correctly speaking – there are three pairs of digits representing red, green, and blue, respectively: There are six digits and a hash symbol ( F is actually a digit, but more on that later). Hexadecimal representation is likely to be the format you encounter most. In these examples, each RGB value is represented using percentile measurements, but there are more popular ways to describe colour when programmings websites and applications. Other named colours include: blue, which is rgb(0%,100%,0%) or orange, which is rgb(100%,60%,0%) and so forth. If you wished for a slightly darker red, you could use rgb(90%,0%,0%). Of course, the keyword “ red” refers to a very specific shade of red, which may not suit your design. Alternatively, CSS allows you to write it out as: Red is one of the standard CSS colour names, so you could simply type “ red”. CSS allows for various means of denoting RGB colour.