blend HSB Colors - Java 2D Graphics

Java examples for 2D Graphics:Color HSB

Description

blend HSB Colors

Demo Code


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

public class Main {
    public static Color blendHSBColors(Color c1, Color c2) {
        float h, s, b;
        float[] tmp = new float[3];
        Color.RGBtoHSB(c1.getRed(), c1.getGreen(), c1.getBlue(), tmp);
        h = tmp[0];/*from   ww w  .  ja  va2  s.  c om*/
        s = tmp[1];
        b = tmp[2];
        Color.RGBtoHSB(c2.getRed(), c2.getGreen(), c2.getBlue(), tmp);
        h = (tmp[0] + h) / 2;
        s = (tmp[1] + s) / 2;
        b = (tmp[2] + b) / 2;

        int a = (c1.getAlpha() + c2.getAlpha()) / 2;
        int c = Color.HSBtoRGB(h, s, b) + (a << 24);
        return getColor(c);
    }

    /**
     * Get the alpha value of a 32-bit ARGB color
     * 
     * @param rgb
     * @return
     */
    public static int getAlpha(int argb) {
        return argb >> 24;
    }

    /**
     * Get the color from a ARGB color value
     * 
     * @param rgb
     * @return
     */
    public static Color getColor(int argb) {
        int a = (argb >> 24) & 255;
        int r = argb >> 16;
        int g = (argb >> 8) & 255;
        int b = argb & 255;
        return new Color(r, g, b, a);
    }
}

Related Tutorials