Java Array Average average(int argb0, int argb1)

Here you can find the source of average(int argb0, int argb1)

Description

Averages two ARGB colors.

License

Apache License

Parameter

Parameter Description
argb0 First color value.
argb1 Second color value.

Return

The averaged ARGB color.

Declaration

public static int average(int argb0, int argb1) 

Method Source Code

//package com.java2s;
/*/*  w ww  .  j  a v  a  2s . c o m*/
 Copyright 2005, 2006 by Gerald Friedland and Kristian Jantz

 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.
 */

public class Main {
    /**
     * Averages two ARGB colors.
     *
     * @param argb0 First color value.
     * @param argb1 Second color value.
     * @return The averaged ARGB color.
     */
    public static int average(int argb0, int argb1) {
        return packPixel((getAlpha(argb0) + getAlpha(argb1)) / 2,
                (getRed(argb0) + getRed(argb1)) / 2,
                (getGreen(argb0) + getGreen(argb1)) / 2,
                (getBlue(argb0) + getBlue(argb1)) / 2);
    }

    /**
     * Limits the values of a,r,g,b to values from 0 to 255 and puts them
     * together into an 32 bit integer.
     *
     * @param a Alpha part, the first byte.
     * @param r Red part, the second byte.
     * @param g Green part, the third byte.
     * @param b Blue part, the fourth byte.
     * @return A ARBG value packed to an int.
     */
    public static int packPixel(int a, int r, int g, int b) {
        if (a < 0) {
            a = 0;
        } else if (a > 255) {
            a = 255;
        }
        if (r < 0) {
            r = 0;
        } else if (r > 255) {
            r = 255;
        }
        if (g < 0) {
            g = 0;
        } else if (g > 255) {
            g = 255;
        }
        if (b < 0) {
            b = 0;
        } else if (b > 255) {
            b = 255;
        }
        return (a << 24) | (r << 16) | (g << 8) | b;
    }

    /**
     * Returns the alpha component of an ARGB value.
     *
     * @param argb An ARGB color value.
     * @return The alpha component, ranging from 0 to 255.
     */
    public static int getAlpha(int argb) {
        return (argb >> 24) & 0xFF;
    }

    /**
     * Returns the red component of an (A)RGB value.
     *
     * @param rgb An (A)RGB color value.
     * @return The red component, ranging from 0 to 255.
     */
    public static int getRed(int rgb) {
        return (rgb >> 16) & 0xFF;
    }

    /**
     * Returns the green component of an (A)RGB value.
     *
     * @param rgb An (A)RGB color value.
     * @return The green component, ranging from 0 to 255.
     */
    public static int getGreen(int rgb) {
        return (rgb >> 8) & 0xFF;
    }

    /**
     * Returns the blue component of an (A)RGB value.
     *
     * @param rgb An (A)RGB color value.
     * @return The blue component, ranging from 0 to 255.
     */
    public static int getBlue(int rgb) {
        return (rgb) & 0xFF;
    }
}

Related

  1. average(final float[] values)
  2. average(final int totalAmount, final int orderCount)
  3. average(final T p_num1, final T p_num2)
  4. average(float[][] data, int startIndex, int endIndex)
  5. average(float[][] originalValues, float[][] newValues, int[] indexArray)
  6. average(int... is)
  7. average(int... values)
  8. average(int[] a)
  9. average(int[] array)