G_shawn G_shawn - 3 months ago 10
Android Question

How to change RGB values based on some Variable ? ( Value Mapped Colors )

I am trying to Map 1.0 to 5.0 float values to RGB Colors programmatically. Below program is working fine for me, But it takes float value and compares with some range and returns hardcoded RGB values.

Instead of this, I want to change RGB values based on the float values. In my logic, I get the same color for 1.1 to 1.9 for sure, But I want dynamically changing RGB based on 1.0 to 5.0.

If I tried shifting R,G,B, but could not do it.

btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String test = "1.8,-0.9,3,4.9,2.3";
String[] sep = test.split(","); //seperating data on ","

et0.setText(sep[0]);
float f0 = Float.parseFloat(sep[0]); // changing string to float to compare in checkdata();
et0.setBackgroundColor(Color.parseColor(checkdata(f0)));

et1.setText(sep[1]);
float f1 = Float.parseFloat(sep[1]);

et1.setBackgroundColor(Color.parseColor(checkdata(f1)));

et2.setText(sep[2]);
float f2 = Float.parseFloat(sep[2]);
et2.setBackgroundColor(Color.parseColor(checkdata(f2)));

et3.setText(sep[3]);
float f3 = Float.parseFloat(sep[3]);
et3.setBackgroundColor(Color.parseColor(checkdata(f3)));

et4.setText(sep[4]);
float f4 = Float.parseFloat(sep[4]);
et4.setBackgroundColor(Color.parseColor(checkdata(f4)));
}
});
}

public String checkdata(float f) {

if (f<1) {

return "#000000" ;
}
if ( f >=1 && f<2) {

return "#febebb" ;
}
if (f >= 2 && f<3) {
return "#fe8f8b" ;
}
if (f >=3 && f<4) {
return "#fe5953" ;
}
if (f >=4 && f<5) {
return "#ff0900" ;
}
else {
return "#000000";
}
}


Any help?

Answer

For Those who were interested in this question !

I used RGB(HEX)'s Blue only (with string separation )and converted B's Hex to integer. Then I used gradient formula. After this float value, I rounded to integer and converted to hex value. This whole Hex -> Int -> Float -> Int -> Hex conversion has done on Blue only.Now I concatenate B to RG and I have the updated RGB based on the variable value.

  public String checkdata(float f) {

        String Picked = ec1.getText().toString();
        Log.i("Color Picked(Hex)",Picked);
        
        String gradSelect = ec1.getText().toString().substring(5);     // picking B from #RRGGBB which is 5th element
        Log.i("Seperated Single(Hex)",gradSelect);
        int intGrad = Integer.parseInt(gradSelect,16);    //Converting Blue to Integer equivalent
        Log.i("Converted to Int", ""+intGrad );

        float float2Grad = f*255/5;    // Mapping Input f's ( 0 to 5 ) values to 0 to 255 color values
       // float float2Grad = (intGrad - f)/ (6-f);     //another formula, but range will not be full 0 to 255 integers of color
        Log.i("Float2Gradient", ""+float2Grad );

        int rounded_i = Math.round(float2Grad);                              //float to int
        Log.i("Rounded Gradient(Int)", ""+rounded_i);

        String updatedBlueHex = Integer.toHexString(rounded_i);              // padding by O for integer values < 15 (Hex 0 to F), so  that RRGGB can be avoided and RRGGBB
        if (updatedBlueHex.length() == 1) {
            updatedBlueHex = "0".concat(updatedBlueHex);                       // if its F then It will give 0F and hence RRGGBB formate maintained
                                }
        Log.i("Updated Single(Hex)",updatedBlueHex);



        String redGreen = Picked.substring(1,5);                                 // RRGG 
        Log.i("Untouched RG(Hex)", redGreen);

        String Returning = redGreen.concat(updatedBlueHex);                                // RRGG + Bb = RRGGBB
        Log.i("Returned String(Hex)",Returning);

        return "#"+Returning; 

    }

This worked for me ! Thanks. Comment If you have any question