颜色变换就是矩阵变换,色彩信息的矩阵表示
- 四阶表示
如果想将色彩(0,255,0,255)更改为半透明时,可以使用下面的的矩阵运算来表示
真正的运算使用五阶矩阵
考虑下面这个变换:
1、红色分量值更改为原来的2倍;
2、绿色分量增加100;
则使用4阶矩阵的乘法无法实现,所以,应该在四阶色彩变换矩阵上增加一个“哑元坐标”,来实现所列的矩阵运算:
安卓中使用ColorFilter进行颜色变换,其中就是利用了色彩矩阵
@Override
protected void onDraw(Canvas canvas) {
//画原图
Paint paint = new Paint();
RectF rectF = new RectF(100, 100, 100 + bitmap.getWidth() / 2, 100+bitmap.getHeight() / 2);
canvas.drawBitmap(bitmap, null, rectF, paint);
//画设置ColorFilter效果后的图
rectF = new RectF(600, 100, 600 +bitmap.getWidth() / 2, 100+bitmap.getHeight() / 2);
//将alpha值变为原来的一半
float[] colorMatrix = new float[]{
1,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,0.5f,0,
};
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
canvas.drawBitmap(bitmap, null, rectF, paint);
}
//平移运算---将red通道加上100
float[] colorMatrix = new float[]{
1,0,0,0,100,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1f,0,
};
// 反相效果--底片效果
float[] colorMatrix = new float[]{
-1,0,0,0,255,
0,-1,0,0,255,
0,0,-1,0,255,
0,0,0,1f,0,
};
//缩放运算---乘法 -- 颜色增强
float[] colorMatrix = new float[]{
1.2f,0,0,0,0,
0,1.2f,0,0,0,
0,0,1.2f,0,0,
0,0,0,1,0,
};
// 黑白照片
// 去色原理:只要把R G B 三通道的色彩信息设置成一样,那么图像就会变成灰色,
// 同时为了保证图像亮度不变,同一个通道里的R+G+B =1
float[] colorMatrix = new float[]{
0.213f, 0.715f,0.072f,0,0,
0.213f, 0.715f,0.072f,0,0,
0.213f, 0.715f,0.072f,0,0,
0,0,0,1,0,
};
// 发色效果---(比如红色和绿色交换)
float[] colorMatrix = new float[]{
0,1,0,0,0,
1,0,0,0,0,
0,0,1,0,0,
0,0,0,1,0,
};
// 复古效果
float[] colorMatrix = new float[]{
1/2f,1/2f,1/2f,0,0,
1/3f, 1/3f,1/3f,0,0,
1/4f,1/4f,1/4f,0,0,
0,0,0,1,0,
};
// 颜色通道过滤
float[] colorMatrix = new float[]{
1,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,1,0,
};