3 ms·
Just for sake of completeness, a walk through the parsing step-by-step shows nothing actually changes until step 10, when the algorithm does its second characte
by ubernostrum 10y ago
Just for sake of completeness, a walk through the parsing step-by-step shows nothing actually changes until step 10, when the algorithm does its second character-replacement pass, replacing characters that aren't hexadecimal digits (they get replaced with zeroes; the first pass did this to any characters outside of Unicode's Basic Multilingual Plane).
At that point 'chucknorris' becomes 'c00c0000000'.
In step 11, the value is padded with zeroes until its length is a non-zero multiple of three. At that point it becomes 'c00c00000000'.
In step 12, the value is split into three equal-length sub-values which will become the red, green and blue components of the color value. They are: (c00c, 0000, 0000).
Steps 13 and 14 then attempt to truncate those values, first by reducing them to eight characters in length and then removing leading zeroes (but only if all three sub-values have a leading zero). These make no changes for the sub-values we've arrived at.
Step 15 performs the final truncation: if the sub-values are still of length greater than two, all but the first two characters are removed. At this point the sub-values are: (c0, 00, 00).
Finally, steps 16-20 convert these from hexdecimal to decimal, and return the value: (192, 0, 0).