Search code examples
androidimage-manipulationcolormatrix

Does Android ColorMatrix setSaturation() support values greater than one?


Looking at the Android docs, the ColorMatrix setSaturation() method says:

Set the matrix to affect the saturation of colors. A value of 0 maps the color to gray-scale. 1 is identity.

I'm trying to boost the saturation (in terms of HSL/HSV) to get more intense colors, so I passed in 1.6 and it seems to work. I've run into device-specific issues with Android layout shadowRadius values less than 1, so is there a danger of those types of issues with this parameter and exceeding the specified range?


Solution

  • The range for the saturation is indeed between 0 and 1. But lets take a look at the code for the setSaturation() method. (Android is open source):

    public void setSaturation(float sat) {
        reset();
        float[] m = mArray;
    
        final float invSat = 1 - sat; //<---------invSat will be negative if sat bigger than 1
        final float R = 0.213f * invSat;
        final float G = 0.715f * invSat;
        final float B = 0.072f * invSat;
    
        m[0] = R + sat; m[1] = G;       m[2] = B;
        m[5] = R;       m[6] = G + sat; m[7] = B;
        m[10] = R;      m[11] = G;      m[12] = B + sat;
    }
    

    The part of the code that we care about is pointed out. As you can see,The method will take your input and subtract it from 1. If you input a value greater than 1, this will result in a negative value for the invSat, and as you can see it may cause bigger problems as well.