Oli
Oli

Reputation: 239918

Creating distinct colours from similar numbers, in a reproducible way

I have a booking system for a carpark. For any given day there is a decimal booking rate.

I need to to display a calendar of the rates over the year so the admins can track where the price hikes are, and to quickly check that things are the right price.

I have had the idea of creating a key of colours and mapping them to the distinct rates we use over the year. It's just a bit of CSS to display those on the calendar. That would be beautiful.

At a glance this is really simple:

But what happens when Bob from management blunders in and adds a new cheap rate? All the rates get shunted up a colour. Bob hasn't actually broken anything but when the page refreshes, it looks like he's changed the entire system. Appearance is important here.

Without a really tiresome storing/retrieval process, is there a good way to "hash" my rates into a smallish set of good distinctive/mapping colours?


Or is this crazy? Should I just reverse-"heat map" this between min and max so the lowest rate shows white, and the highest a dark red? That's easy to do and Bob's blunders will still have an effect, but they won't rotate the entire bank of colours.

The problem with a heatmap is numbers that are close together in a large range will show as practically the same colour. That's sort of the whole problem I'm trying to solve ☹

Upvotes: 2

Views: 75

Answers (1)

Oli
Oli

Reputation: 239918

Yup, the hash/bitwise answers on this question seem to do the right thing.
Here's my python implementation:

def colourise(s):
    # Returns a RGB tuple.
    h = hash(str(s))
    return ((h & 0xFF0000) >> 16), ((h & 0x00FF00) >> 8), (h & 0x0000FF)

Upvotes: 1

Related Questions