开发者

Mapping integers to darkish colours

I'm wondering how to go about mapping integers to colours, but doing it such that the sequence doesn't have much of an effect on the colour (although it really doesn't matter much) and also so that plugging the same number into the function over and over will give the same colour.

It's for a JTable, and colouring the row of the table to highlight those similarly that have the same ID. Storing any metadata means lots of messing about and inefficiency, which I'd like to avoid.

Basically, f(x)->c for all x in the positive range of integers such that f(x)->c1, f(x+1)->c2, c1 !~ c2, where x is the id, and c is the resulting colour. I had thought about using Random, but that changes based on the sequence of generating numbers. Without the sequentiality added in, it'd be okay.

I forgot to mention, the colours should be dar开发者_Go百科kish so that the white text will show through properly.

Cheers,

Chris


Use a hash function to calculate the RGB components from your integer. If you're using the usual 1 byte per component, then generate a 3 byte hash value. Any decent hash value will ensure that similar inputs will result in non-similar hash values.

To make sure the colours are dark enough, you could fix one of they bits (e.g. the 5th) at 1 in each colour component. Or you could use the HSB scheme, fixing the darkness level.


Given your desire for an automated approach that produces "darkish" colors, I would look to Color.getHSBColor(), and change the hue while keeping the saturation and brightness the same. You may have to cut out the section of the color wheel that represents yellows.


Also consider the brighter() and darker() methods of Color. This example uses the former in it's FocusHandler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜