ColorUtils.java Source code

Java tutorial

Introduction

Here is the source code for ColorUtils.java

Source

/*
 * ColorUtils.java Created Nov 17, 2010 by Andrew Butler, PSL
 */
//package prisms.util;

import java.awt.Color;

/** A set of tools for analyzing and manipulating colors */
public class ColorUtils {

    /**
     * Performs a somewhat subjective analysis of a color to determine how dark it looks to a user
     * 
     * @param color The color to analyze
     * @return The darkness of the color
     */
    public static float getDarkness(Color color) {
        float ret = color.getRed() + color.getGreen() + color.getBlue() / 10;
        ret /= (255 + 255 + 255 / 10);
        ret = 1 - ret;
        final float lightDarkBorder = 0.7f;
        if (ret > lightDarkBorder)
            ret = 0.5f + (ret - lightDarkBorder) * 0.5f / (1 - lightDarkBorder);
        else
            ret = ret * 0.5f / lightDarkBorder;
        return ret;
    }
}