Java Color Lighten lighter(Color color, float ratio)

Here you can find the source of lighter(Color color, float ratio)

Description

Make a color lighter.

License

Open Source License

Parameter

Parameter Description
color Color to mix with white.
ratio White ratio (1.0 = complete white, 0.0 = color).

Return

Lighter color.

Declaration

public static Color lighter(Color color, float ratio) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:// w w  w. j ava 2 s  .com
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.awt.Color;

public class Main {
    /**
     * Make a color lighter.
     * 
     * @param color
     *          Color to mix with white.
     * @param ratio
     *          White ratio (1.0 = complete white, 0.0 = color).
     * @return Lighter color.
     */
    public static Color lighter(Color color, float ratio) {
        return mergeColors(Color.WHITE, ratio, color, 1 - ratio);
    }

    /**
     * Merges two colors. The two floating point arguments specify "how much" of the corresponding color is added to the
     * resulting color. Both arguments should (but don't have to) add to <code>1.0</code>.
     * <p>
     * This method is null-safe. If one of the given colors is <code>null</code>, the other color is returned (unchanged).
     */
    public static Color mergeColors(Color a, float fa, Color b, float fb) {
        if (a == null) {
            return b;
        }
        if (b == null) {
            return a;
        }
        return new Color((fa * a.getRed() + fb * b.getRed()) / (fa + fb) / 255f,
                (fa * a.getGreen() + fb * b.getGreen()) / (fa + fb) / 255f,
                (fa * a.getBlue() + fb * b.getBlue()) / (fa + fb) / 255f);
    }
}

Related

  1. lighter(Color c, boolean transparant)
  2. lighter(Color c, float factor)
  3. lighter(Color clr)
  4. lighter(Color clr, double saturationFraction)
  5. lighter(Color color, double fraction)
  6. lighter(final Color color, final int rgbOffset)
  7. lighter(final Color color, float factor)
  8. lighterColor(Color c, double amount)
  9. lighterColor(Color lineColor, int adj)