Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Creative presentation algorithm: IP to a color

by Ven'Tatsu (Deacon)
on Aug 29, 2005 at 15:27 UTC ( #487439=note: print w/replies, xml ) Need Help??

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; }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://487439]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2022-08-10 02:49 GMT
Find Nodes?
    Voting Booth?

    No recent polls found