Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert color code to float array in android

colors resource file:

<color name="red">#ffff5454</color>

code:

int color = Resources.getColor(getResources().Color.red);

// how to get paramArrayOfFloat from color?

ColorMatrix localColorMatrix = new ColorMatrix();
localColorMatrix.setSaturation(0.0F);
localColorMatrix.set(paramArrayOfFloat);

how can I get paramArrayOfFloat from my color number? I want to convert my number to float array. Is this possible in android?

like image 325
senzacionale Avatar asked Feb 02 '26 13:02

senzacionale


1 Answers

ColorMatrix:

4x5 matrix for transforming the color+alpha components of a Bitmap. The matrix is stored in a single array, and its treated as follows: [ 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 (after clamping) 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;

For red color -

 float[] colorTransform = {
        0, 1f, 0, 0, 0, 
        0, 0, 0f, 0, 0,
        0, 0, 0, 0f, 0, 
        0, 0, 0, 1f, 0};

ColorMatrix localColorMatrix = new ColorMatrix();
localColorMatrix.setSaturation(0.0F); //Remove Colour 
localColorMatrix.set(colorTransform); //Apply Red say

For more check: ColorMatrixSample and ColorMatrix.

NOTE:

You can check the link for converting-a-web-color-to-an-rgb-float-array-for-after-effects.

like image 131
My God Avatar answered Feb 05 '26 04:02

My God



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!