颜色分量转换颜色分量转换 记得我在上学的时候老师给我们出了一道代码题就是把一个颜色当中的三个颜色分量都提取出来,然后把当中R和G的值,相互调换位置,从而形成一个新的颜色。 但是由于当时我在上课的时候基本上都处在深度睡眠当中,课余时间各种促进男女同学之间友谊的娱乐活动不断,导致我别说是用代码实现了,就连最基本的题目意思我都没搞明白是什么东西,于是我就一直用各种理由推脱,最后就不了了之了,真是呜呼哀哉,老师没事找事。 一直到工作之后我才理解什么是颜色分量。今天闲来无事突然想起这件趣事,一时兴起和大家分享一下它的实现方法。如果大家...
* Title: ColorOperation *
** Description:将颜色值的红色分量和绿色分量进行互换的微案例代码 *
** Copyright: Copyright (c) 2016 *
** Company: ChinaSoft International Ltd. *
* * @author etc * @version 1.0 */ public class ColorOperation { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // 定义用于测试的原始颜色信息 //11111111 11001101 00111101 01111011 int color = 0xFFCD3D7B; // 定义工具对象 ColorOperation co = new ColorOperation(); // 转换颜色信息后输出 System.out.println("0x" + Integer.toHexString(co.r2g(color)).toUpperCase()); } /** * 将参数中的颜色数据的红色分量和绿色分量进行互换 * * @param color * 需要进行转换的颜色 * @return 转换后的结果 * */ public int r2g(int color) { // 获取原始颜色中的红色分量值 0xFF CD 3D 7B 11001101 //System.out.println(Integer.toHexString(color << 8)); //System.out.println(Integer.toHexString(color << 8 >>> 24)); int r = color << 8 >>> 24; // 获取原始颜色中的绿色分量值 //System.out.println(Integer.toHexString(color << 16)); //System.out.println(Integer.toHexString(color << 16 >>> 24)); int g = color << 16 >>> 24; // 将颜色中的红色和绿色部分清空 color = color & 0xFF0000FF; // 将绿色分量写入到红色分量的位置,将红色分量值写入到绿色分量的位置 color = color | (g << 16) | (r << 8); // 返回转换后的结果 return color; } }