Returns a Color for an object that is determined by the object's hash code. - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Returns a Color for an object that is determined by the object's hash code.

Demo Code


//package com.java2s;
import java.awt.Color;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object value = "java2s.com";
        System.out.println(hashColor(value));
    }// www . j  a v a  2 s  . co m

    /**
     * Returns a Color for an object that is determined by the object's hash code.
     * @param value Object
     * @return a Color for an object that is determined by the object's hash code.
     */
    public static Color hashColor(Object value) {
        if (value == null) {
            return Color.WHITE.darker();
        } else {
            int hash = Math.abs(value.hashCode());
            int r = 0xff - (hash % 0xce);
            int g = 0xff - (hash % 0xdd);
            int b = 0xff - (hash % 0xec);
            Color color = (new Color(r, g, b));
            //Return a brighter color unless the brighter color is white
            return color; // (!Color.WHITE.equals(color.brighter()) ? color.brighter() : color);
        }
    }
}

Related Tutorials