Posterize a given pixel - Android Graphics

Android examples for Graphics:Pixel

Description

Posterize a given pixel

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot.//from www .  j a  va 2  s.c  om
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;

public class Main {
    /**
     * Posterize a given pixel
     * 
     * @param argb
     *            color
     * @param depth
     *            of posterization
     * @return
     */
    public static int posterizePixel(int argb, int depth) {
        int noOfAreas = 256 / depth;
        int noOfValues = 256 / (depth - 1);

        int a = (argb >> 24) & 0xff;
        int r = (argb >> 16) & 0xff;
        int g = (argb >> 8) & 0xff;
        int b = argb & 0xff;

        int redArea = r / noOfAreas;
        r = noOfValues * redArea;

        int greenArea = g / noOfAreas;
        g = noOfValues * greenArea;

        int blue = b / noOfAreas;
        b = noOfValues * blue;

        return toRGB(a, r, g, b);
    }

    /**
     * 
     * @param alpha
     * @param red
     * @param green
     * @param blue
     * @return
     */
    public static int toRGB(int alpha, int red, int green, int blue) {
        return (alpha & 0xff) << 24 | (red & 0xff) << 16
                | (green & 0xff) << 8 | (blue & 0xff) << 0;
    }
}

Related Tutorials