2

I need process a hex string value to floating color.

The real color is (in RGBA format)

{ 0.63671875f, 0.76953125f, 0.22265625f, 1.0f } 

Is green, when 0 is equivalent to 00 and 1 is FF. But need convert the color to 0-255 decimal to calculate the % from 0 to 255 and set the float value.

How to transform by example #AABBCCFF to float array?

When parsecolor i have the integer value:

Color.parseColor("#AABBCCFF"); 

But how transform the integer to each color in float? red green blue and alpa

1 Answer 1

6

I fount the solution:

private float[] getFloatArrayFromARGB(String argb){ int color_base = Color.parseColor(argb); int red = Color.red(color_base); int green = Color.green(color_base); int blue = Color.blue(color_base); int alpha = Color.alpha(color_base); return new float[]{ (red / 255f), (green / 255f), (blue / 255f), (alpha / 255f) }; } 

Use:

String argb = "#FFFF0000"; float[] color = this.getFloatArrayFromARGB(argb); GLES20.glUniform4fv(mColorHandle, 1, color, 0); 

:D Now use ARGB format to paint 3D objects with OpenGL

Sign up to request clarification or add additional context in comments.

3 Comments

Multiplying and dividing by 100 is not necessary: (((red * 100) / 255) / 100) can be simplified to red / 255f
(((red * 100) / 255) / 100) also uses integer division and thus would only return 0 or 1
Richard, i tested, when divide two integer in Java, the framework transform the result from integer to float automaticaly. The result is parsed by new float[], the result return an array of float values. Thanks for simplify the result deleting the "100".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.