Question

I know that in binary, an ipv4 address is 32 bits, and web colors are 24 bit. I also know because of that, there is not a unique color for every ip address.

I was able to hack together a solution by removing the first dotted quad, leaving a dotted triplet, and just using those for the R,G,B.

What I would prefer, and what I can't seem to wrap my head around right now is how to 'degrade' the 32 bit value into 24 bits, then get a hex value from that.

Does it even make sense to divide the decimal representation of an ipv4 address by 256, then convert that to hex?

I really hope that makes sense and I'm not abusing too much terminology.

Any programming language is fine, but bonus for javascript solution.

Was it helpful?

Solution 2

Pragmatic solution: Just create a quick to compute but hard to predict hash like MD5* from the IP address and use only the first 24 bits of the result.

This will discard some of the entropy of the input, but there is as good as no bias for which bits are discarded. There will be collisions, but there will be no discernable pattern which addresses will collide.

*yes, MD5 is obsolete as a cryptographic hash function, but this doesn't matter at all in this case. It's relatively easy to implement, executes quickly and creates seamingly chaotic results, and that's all that is needed in this case.

OTHER TIPS

Maybe you should make a proportion. How large is the IP address (from 0 to 2^32), you should put your unknown X into the same proportion from 0 to 2^24.

So, basically if your address is A and your color is C, the proportion should be:

A/2^32 == C/2^24.

That's just a basic idea. Do you want to make colors from the whole IP address, or maybe take each octet separately? What are you trying to achieve precisely?

An IP address has 32 bit, an RGB value only has 24 bit. That means that you have to discard some data. It would make sense to map the bits in a matter that the most significant bits in the IP address map to the most significant bits of the color space.

IP address are organized in a hierarchical manner. Large contiguous blocks are assigned to registrars of countries, which then split these blocks and assign them to ISPs, which then again split these blocks and assign them to companies, or to groups of clients, often based on geographical proximity. That means the first byte is the most significant and the last one is the least significant.

In RGB values, on the other hand, each color channel has technically-speaking equal significance. Technically. The human eye is most perceptive to green and least perceptive to blue, so green is more significant than red which is more significant than blue. In each color-channel, the significance of bits decreases.

That means I would map the 32 bit of the IP address to RGB in this manner:

12345678 90123456 78901234 56789012
GRBGRBGR BGRBGRBG RBGRBGRB --------

That means addresses from different networks will be colored very differently and addresses from the same subnet in similar shades. The smaller the distance between two addresses in network topology, the smaller their color difference. Addresses from the same /24 subnet will have exactly the same color. But when two IPs come from the same /24 subnet, they very likely belong to two machines from the same organization or come from an ISP address pool which is dynamically assigned to clients living in a rather small georgraphical area, so the chance that you are dealing with the same person is pretty high.

One possibility is to first convert your IP-address to CMYK color, each for range 0-255. Then, convert your CMYK color to RGB.

Or.

Perhaps take the first three bytes from IP-address and use that for RGB and use the last byte for alpha-channel normalized in range 0-1.

As far as i know, then the highest value in RGB is FF in each color, which corresponds to 255.

And IPv4 has a highest value of 255 in each notation.

And it would be wise to discard the first notation, since some might be reserved.

So the color for 255.255.255.255 should be RGB (255, 255, 255).

And you might have 255 (minus the reserved ones) possibilities that the color return.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top