http://qs1969.pair.com?node_id=487439


in reply to Creative presentation algorithm: IP to a color

From a usability stand point I think that you don't want to use the full range of RGB colors. Otherwise you may have a color like #F0E7FA appear on a white page and be nearly invisable.

for example: #F0E7FA

The easyest way to do that would be to set the most significant bit of each byte of the RGB value to 0 for light backgrounds and 1 for dark backgrounds. This limits the color space to only 21 bits, however setting only one of the most significant bits opposite of the others doesn't move the color too far to make text illegable to me, so picking one or none of the 3 bits gives 4 options, or to look at it another way restores 2 bits to the color space without sacrificing readability of the text.

In my code I do this by using the last 3 bytes of the IP address for the base RGB values, shifting off the least significant bit. Then take the first byte and and xor each 2 bit group with the others to get a 2 bit number to select which byte to set the most significant bit. Finely a flag toggles all the most significant bits if the text should be bright. I think this would effectivly hide the IP address while still keeping many 'close' ip addresses close in color. (though the choice of using the first byte of the IP address to affect the most significant bits by xoring it together will place many distant IP addresses near each other in the color space.)

sub ip2rgb { my $ip = shift; my $bright = shift; my @octets = split /\./, $ip; my $mso = shift @octets; my @rgb = map { $_ >> 1 } @octets; my $msb = ($mso >> 6 & 0x03) ^ ($mso >> 4 & 0x03) ^ ($mso >> 2 & 0 +x03) ^ ($mso >> 2 & 0x03) ^ ($mso & 0x03); if ($msb != 3) { $rgb[$msb] |= 0x80; } if ($bright) { for (@rgb) { $_ ^= 0x80; } } return @rgb; }