2016-08-19 4 views
1

J'essaie de mapper par programmation des valeurs flottantes de 1.0 à 5.0 aux couleurs RVB. Le programme ci-dessous fonctionne bien pour moi, mais il prend une valeur flottante et compare avec une certaine plage et renvoie des valeurs RVB codées en dur. Au lieu de cela, je veux changer les valeurs RVB en fonction des valeurs flottantes. Dans ma logique, je reçois la même couleur pour 1.1 à 1.9, mais je veux changer dynamiquement RGB basé sur 1.0 à 5.0.Comment changer les valeurs RVB en fonction de certaines variables? (Couleurs mappées de la valeur)

Si j'ai essayé de déplacer R, G, B, mais que je n'ai pas pu le faire.

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"; 
    } 
} 

Une aide?

Répondre

0

Pour ceux qui se sont intéressés à cette question!

J'ai utilisé le bleu RGB (HEX) uniquement (avec séparation de chaînes) et l'hex de B converti en nombre entier. Ensuite, j'ai utilisé le gradient formula. Après cette valeur float, j'ai arrondi à entier et converti en valeur hexadécimale. Cette conversion Hex -> Int -> Float -> Int -> Hex a fait sur Blue seulement. Maintenant, je concatène B à RG et j'ai le RVB mis à jour basé sur la valeur de la variable.

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; 
 

 
    }

Cela a fonctionné pour moi! Merci. Commentaire Si vous avez des questions

0

voulez-vous gradient? si oui, https://stackoverflow.com/a/33466114/2061650 vous aider

if (f >=1 && f<2) { 

     return "#febebb" ; 
    } 
    if (f >= 2 && f<3) { 
     return "#fe8f8b" ; 
    } 

->

if (f >=1.5 && f<2.5) { 
    c1 = C.pc("#febebb"); 
    c2 = C.pc("#fe8f8b"); 
    return evaluator.evaluate((f-1.5) /(2.5-1.5), c1, c2) 

} 

et

et4.setBackgroundColor(Color.parseColor(checkdata(f4))); 

->

et4.setBackgroundColor(checkdata(f4)); 
+0

Oui, je suis à la recherche d'un dégradé. J'ai 25 valeurs différentes dans edittext et il devrait représenter la couleur de fond basée sur les valeurs dans le edittxt –

+0

s'il vous plaît lire la réponse mise à jour. Tout est clair? –

+0

Je pense (f-1.5) /(2.5-1.5) est double, mais nécessaire est flottant. Et évaluer renvoie un objet non entier. Mais va vérifier à nouveau. –