Matrix(三) 神奇的ColorMatrix(图片的颜色与色相修改)

之前整了两章关于Android中的Matrix操作,本章来谈一谈Android中的另一个Matrix-ColorMatrix。

本文讲述的具体内容如图:

Color.jpg

**如果不喜欢看描述的话,项目的demo地址如下:
https://github.com/MartinBZDQSM/Matrix
**

ColorMatrix

谈到ColorMatrix,那么先聊聊Android中颜色.
我们在经常调整布局颜色的时候使用的是为八位的十六进制表示法的颜色值,例如:#0xd5d5d5;这种表示法也正对应着Android中使用的ARGB模式,也就是说颜色和透明度 (alpha) 的值都用以十六进制表示法表示。其首两位代表着透明度剩下的分别对应着红绿蓝三种颜色,其中任何一种颜色的值范围都是 0到 255(00到 ff)。

而Android支持的颜色模式都有:

颜色模式 说明
ARGB8888 四通道高精度(32位)
ARGB4444 四通道低精度(24位)
RGB565 三通道(16位)
Alpha8 透明通道(8位)

所以看到这里,我们其实就能猜出来,为什么一张好好的透明PNG图片在某些加载框架中会变黑?正式因为其框架使用的默认通道是RGB565 缺少了一个透明通道,所以当我们修改了图片的颜色模式为ARGB8888或者ARGB4444后会变正常。

现在我们来看看ColorMatrix 的官方描述:

/**
 * 4x5 matrix for transforming the color and alpha components of a Bitmap. 
 * The matrix can be passed as single array, and is treated as follows:
 * 下面这个4x5的矩阵可以表达,为一张图的颜色以及透明图信息。
 *  [ a, b, c, d, e,
 *    f, g, h, i, j,
 *    k, l, m, n, o,
 *    p, q, r, s, t ]
 *
 * When applied to a color  颜色分量矩阵
 *   [R, G, B, A]
 *  the resulting color is computed as:计算结果如下公式:
 *
 *   R’ = a*R + b*G + c*B + d*A + e;
 *   G’ = f*R + g*G + h*B + i*A + j;
 *   B’ = k*R + l*G + m*B + n*A + o;
 *   A’ = p*R + q*G + r*B + s*A + t;
 *
 * That resulting color 
 *  [R’, G’, B’, A’]
 * then has each channel clamped to the 0 to 255 range.计算出来的结果,每个通道都在0-255之间
 * 
 * The sample ColorMatrix below inverts incoming colors by scaling each
 * channel by -1, and then shifting the result up by
 * 255 to remain in the standard color space.
 * 好像用来举例说明的,前四列代表RGBA的偏向成分,第五列代表着颜色偏移量
 *
 *   [ -1, 0, 0, 0, 255,
 *     0, -1, 0, 0, 255,
 *     0, 0, -1, 0, 255,
 *     0, 0, 0, 1, 0 ]
 */

总得来说就是,一张图可以通过设置 颜色矩阵 来改变它的颜色效果。

1.颜色偏向

为了测试ColorMatrix能够影响图片的颜色效果,我设置了4个SeekBar,来分别代表着R,G,B,A四个通道,其范围都是0-255.

 @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        //设置偏移量
        colorMatrix.setScale(caculate(seekBarR.getProgress()), caculate(seekBarG.getProgress()),
                caculate(seekBarB.getProgress()), caculate(seekBarA.getProgress()));

       //偏向颜色的值
        colorText.setText("颜色值:#" + Integer.toHexString(seekBarA.getProgress())
                + Integer.toHexString(seekBarR.getProgress())
                + Integer.toHexString(seekBarG.getProgress())
                + Integer.toHexString(seekBarB.getProgress()));

         //用来显示偏向的颜色
        colorView.setBackgroundColor(Color.argb(seekBarA.getProgress(),
                seekBarR.getProgress(),
                seekBarG.getProgress(),
                seekBarB.getProgress()));

        //为组件设置新的过滤器
        imageView.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        
    }

效果演示如下:


ColorMartix.gif

2.色相

本来还不是很明白色相指的什么,查查百度才知道:色相是色彩的首要特征,是区别各种不同色彩的最准确的标准。
正如PS中使用的的色相/饱和度功能,它可以通过修改颜色的三个基本属性:色相彩度明度,来进行调节色彩。

PS_HUE.png

那么,在Android中如何做呢?同样 我写了三个SeekBar范围值也分别是0-255。

  @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        float mHueValue = (seekBarHue.getProgress() - 128f) * 1.0f / 128f * 180;
        float mSaturationValue = seekBarSaturation.getProgress() / 128f;
        float mLightnessValue = seekBarLightness.getProgress() / 128f;

        //设置色相
        mHueMatrix.reset();
        mHueMatrix.setRotate(0, mHueValue);
        mHueMatrix.setRotate(1, mHueValue);
        mHueMatrix.setRotate(2, mHueValue);

        //设置饱和度
        mSaturationMatrix.reset();
        mSaturationMatrix.setSaturation(mSaturationValue);

        //亮度
        mLightnessMatrix.reset();
        mLightnessMatrix.setScale(mLightnessValue, mLightnessValue, mLightnessValue, 1);

        colorMatrix.reset();// 效果叠加
        colorMatrix.postConcat(mLightnessMatrix);
        colorMatrix.postConcat(mSaturationMatrix);
        colorMatrix.postConcat(mHueMatrix);

        imageView.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
    }

效果如下:


ColorHue.gif

3.简单的颜色滤镜封装

通过上方的举例,我们很容易将调节过后的偏移矩阵,封装成一些滤镜使用。
我找了一些已经设置好了的偏移矩阵作为滤镜使用,具体效果如图:

ColorFilter.png

下面贴出我封装的固定颜色偏向滤镜和固定偏移矩阵滤镜的方法:

 /**
     * 为imageView设置颜色滤镜
     *
     * @param imageView
     * @param colormatrix
     */
    public static void imageViewColorFilter(ImageView imageView, float[] colormatrix) {
        setColorMatrixColorFilter(imageView, new ColorMatrixColorFilter(new ColorMatrix(colormatrix)));
    }

    /**
     * 为imageView设置颜色偏向滤镜
     *
     * @param imageView
     * @param color
     */
    public static void imageViewColorFilter(ImageView imageView, int color) {
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setScale(Color.alpha(color), Color.red(color), Color.green(color), Color.blue(color));
        setColorMatrixColorFilter(imageView, new ColorMatrixColorFilter(colorMatrix));
    }


    /**
     * 生成对应颜色偏向滤镜的图片,并回收原图
     *
     * @param bitmap
     * @param color
     * @return
     */
    public static Bitmap bitmapColorFilter(Bitmap bitmap, int color) {
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setScale(Color.alpha(color), Color.red(color), Color.green(color), Color.blue(color));
        return setColorMatrixColorFilter(bitmap, new ColorMatrixColorFilter(colorMatrix), true);
    }

    /**
     * 生成对应颜色滤镜的图片,并回收原图
     *
     * @param bitmap
     * @param colormatrix
     * @return
     */
    public static Bitmap bitmapColorFilter(Bitmap bitmap, float[] colormatrix) {
        return setColorMatrix(bitmap, colormatrix, true);
    }

    /**
     * 生成对应颜色滤镜的图片
     *
     * @param bitmap
     * @param colormatrix
     * @param isRecycle
     * @return
     */
    public static Bitmap setColorMatrix(Bitmap bitmap, float[] colormatrix, boolean isRecycle) {
        return setColorMatrixColorFilter(bitmap, new ColorMatrixColorFilter(new ColorMatrix(colormatrix)), isRecycle);
    }


    public static void setColorMatrixColorFilter(ImageView imageView, ColorMatrixColorFilter matrixColorFilter) {
        imageView.setColorFilter(matrixColorFilter);
    }

    public static Bitmap setColorMatrixColorFilter(Bitmap bitmap, ColorMatrixColorFilter matrixColorFilter, boolean isRecycle) {
        Bitmap resource = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColorFilter(matrixColorFilter);
        Canvas canvas = new Canvas(resource);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (isRecycle)
            BitmapUtils.destroyBitmap(bitmap);
        return resource;
    }

包括之前的Matrix的代码都在Git上的demo里有详细的举例,就先写到这里把,应该也没有什么好整理的了。过几天会可能会把之前的Android海报制作弄完。
喜欢这边文章的话,别忘了点个赞哈。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,511评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,495评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,595评论 0 225
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,558评论 0 190
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,715评论 3 270
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,672评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,112评论 2 291
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,837评论 0 181
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,417评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,928评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,316评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,773评论 2 234
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,253评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,827评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,440评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,523评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,583评论 2 249

推荐阅读更多精彩内容