Java Color Brighten brightness(Color c, float scale)

Here you can find the source of brightness(Color c, float scale)

Description

Adjusts the brightness of the given color.

License

Apache License

Parameter

Parameter Description
c The color to adjust
scale The factor (a value from -1 to 1)

Declaration

public static Color brightness(Color c, float scale) 

Method Source Code


//package com.java2s;
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./*from  ww w .j a v  a2s  .com*/
 */

import java.awt.Color;

public class Main {
    /**
     * Adjusts the brightness of the given color.
     * @param c The color to adjust
     * @param scale The factor (a value from -1 to 1) 
     */
    public static Color brightness(Color c, float scale) {
        int r = c.getRed();
        int g = c.getGreen();
        int b = c.getBlue();
        if (r == 0 && g == 0 && b == 0 && scale > 0) {
            r = g = b = Math.min(255, (int) (255 * scale));
        } else {
            r = Math.max(0, (int) (r * scale));
            g = Math.max(0, (int) (g * scale));
            b = Math.max(0, (int) (b * scale));
            if (r > 255 || g > 255 || b > 255) {
                return c;
            }
        }
        return new Color(r, g, b);
    }
}

Related

  1. brighter(Color color)
  2. brighter(Color color, double factor)
  3. brighter(Color color, float factor)
  4. brighterColor(Color c)
  5. brighterColor(String hexValue)
  6. brightness(java.awt.Color c)
  7. brightnessToAlpha(Image image, float alpha)
  8. changeBrightness(Color c, int percent)
  9. changeColorBrightness(final Color color, final double delta)