Converts a float value between 0f and 1f to an int value between 0 and 255 - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Converts a float value between 0f and 1f to an int value between 0 and 255

Demo Code

/*/*  w  w w. j a va  2s.c  o m*/
 * This library is dual-licensed: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation. For the terms of this
 * license, see licenses/gpl_v3.txt or <http://www.gnu.org/licenses/>.
 *
 * You are free to use this library under the terms of the GNU General
 * Public License, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * Alternatively, you can license this library under a commercial
 * license, as set out in licenses/commercial.txt.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        float alpha = 2.45678f;
        System.out.println(floatAlphaToIntAlpha(alpha));
    }

    /**
     * Converts a float value between 0f and 1f to an int value
     * between 0 and 255
     * @param alpha value between 0f and 1f
     * @return int value between 0 and 255
     */
    public static int floatAlphaToIntAlpha(float alpha) {
        if (alpha < 0f || alpha > 1f) {
            throw new IllegalArgumentException(
                    "alpha must be between 0f and 1f. value: " + alpha);
        }
        return (int) (255 * alpha);
    }
}

Related Tutorials