make RGB Light Value - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

make RGB Light Value

Demo Code

/**// www .  j  a v  a  2s. c o  m
 * This class was created by <Vazkii>. It's distributed as
 * part of the Botania Mod. Get the Source Code in github:
 * https://github.com/Vazkii/Botania
 * 
 * Botania is Open Source and distributed under the
 * Botania License: http://botaniamod.net/license.php
 * 
 * File Created @ [Dec 26, 2014, 7:26:31 PM (GMT)]
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        float r = 2.45678f;
        float g = 2.45678f;
        float b = 2.45678f;
        float currentLightValue = 2.45678f;
        System.out.println(makeRGBLightValue(r, g, b, currentLightValue));
    }

    public static int makeRGBLightValue(float r, float g, float b,
            float currentLightValue) {
        // Clamp color channels
        if (r < 0.0f)
            r = 0.0f;
        else if (r > 1.0f)
            r = 1.0f;

        if (g < 0.0f)
            g = 0.0f;
        else if (g > 1.0f)
            g = 1.0f;

        if (b < 0.0f)
            b = 0.0f;
        else if (b > 1.0f)
            b = 1.0f;

        int brightness = (int) (currentLightValue * 15.0f);
        brightness &= 0xf;

        return brightness | ((int) (15.0F * b) << 15)
                + ((int) (15.0F * g) << 10) + ((int) (15.0F * r) << 5);
    }
}

Related Tutorials