Multiplies each of component of colour with amount and divides the result by 256. - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Multiplies each of component of colour with amount and divides the result by 256.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int colour = 2;
        int amount = 2;
        System.out.println(multiply(colour, amount));
    }/*from   www  . ja v a  2  s  .c  o m*/

    /**
     * Multiplies each of component of <code>colour</code> with
     * <code>amount</code> and divides the result by 256.
     * 
     * @param colour The RGB colour to multiply.
     * @param amount The amount to multiply each colour band with, where 256
     *    leaves the colour unchanged.
     * @return The multiplied colour.
     */
    public static int multiply(int colour, int amount) {
        if (amount == 256) {
            return colour;
        }
        int red = (colour & 0xFF0000) >> 16;
        int green = (colour & 0x00FF00) >> 8;
        int blue = colour & 0x0000FF;
        red = red * amount;
        if (red > 65535) {
            red = 65535;
        }
        green = green * amount;
        if (green > 65535) {
            green = 65535;
        }
        blue = blue * amount;
        if (blue > 65535) {
            blue = 65535;
        }
        return ((red << 8) & 0xFF0000) | (green & 0x00FF00)
                | ((blue >> 8) & 0x0000FF);
    }
}

Related Tutorials