一 概述
前提:
Java实现根据分量计算rgb值,并分离rgb分量_百度知道 (baidu.com)https://zhidao.baidu.com/question/2079674468685898308.html
二 计算RGB的值
代码实例:
import java.awt.*;
public class RGB {
public static void main(String[] args) {
rgb();
}
public static void rgb(){
// 方法1: 一个整数转换成 Color ,然后获得r g b的值 ,优点:好记忆
int rgb = 33324442;
Color c = new Color(rgb);
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
System.out.println(c);
System.out.println("红=" + r + "\t绿=" + g + "\t蓝=" + b);
// 方法2: 对整数直接进行计算得到rgb值
int rgb2 = 33324442;
int r1 = (rgb2 >> 16) & 0xFF;
int g1 = (rgb2 >> 8) & 0xFF;
int b1 = (rgb2 >> 0) & 0xFF;
System.out.println("红="+r1+"\t绿="+g1+"\t="+b1);
}
}
结果: