Java Color Lighten lighten(Color color)

Here you can find the source of lighten(Color color)

Description

lighten

License

Open Source License

Parameter

Parameter Description
color a parameter

Return

Increases brightness, and decreases saturation. If factor < 1 it will get darker.

Declaration

public static Color lighten(Color color) 

Method Source Code

//package com.java2s;
/* //from  w ww . j av a2s . co  m
    
 Created on Mar 4, 2005
    
 The Bungee View applet lets you search, browse, and data-mine an image collection.  
 Copyright (C) 2006  Mark Derthick
    
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.  See gpl.html.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
 You may also contact the author at 
 mad@cs.cmu.edu, 
 or at
 Mark Derthick
 Carnegie-Mellon University
 Human-Computer Interaction Institute
 Pittsburgh, PA 15213
    
 */

import java.awt.Color;

public class Main {
    private static final float colorComponentChangeFactor = 2.0f;

    /**
     * @param color
     * @return Increases brightness, and decreases saturation. If factor < 1 it
     *         will get darker.
     */
    public static Color lighten(Color color) {
        return lighten(color, colorComponentChangeFactor);
    }

    /**
     * @param color
     * @param factor
     * @return Multiplies brightness by factor, and divides saturation by
     *         factor. If factor < 1 it will get darker.
     */
    public static Color lighten(Color color, float factor) {
        if (factor <= 0.0)
            factor = colorComponentChangeFactor;
        float[] hsb = getHSBcomponents(color);
        return Color.getHSBColor(hsb[0], Math.min(1.0f, hsb[1] / factor), Math.min(1.0f, hsb[2] * factor));
    }

    public static float[] getHSBcomponents(Color color) {
        float[] result = new float[3];
        Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), result);
        return result;
    }

    public static Color getHSBColor(float h, float s, float b, float alpha) {
        Color c = Color.getHSBColor(h, s, b);
        return new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (alpha * 255 + 0.5));
    }
}

Related

  1. lighten(Color color, double strength)
  2. lighten(final Color color, final int amount)
  3. lighten(int r, int g, int b, double percent)
  4. lightenColor(Color col, float factor)