Here you can find the source of average(int argb0, int argb1)
Parameter | Description |
---|---|
argb0 | First color value. |
argb1 | Second color value. |
public static int average(int argb0, int argb1)
//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; } }