如何根据某些变量更改 RGB 值? (值映射颜色)
How to change RGB values based on some Variable ? ( Value Mapped Colors )
我正在尝试以编程方式将 1.0 到 5.0 浮点值映射到 RGB 颜色。下面的程序对我来说工作正常,但它采用浮点值并与一些范围和 returns 硬编码 RGB 值进行比较。
相反,我想根据浮点值更改 RGB 值。按照我的逻辑,我肯定会得到 1.1 到 1.9 的相同颜色,但我想根据 1.0 到 5.0 动态更改 RGB。
如果我尝试移动 R、G、B,但无法做到。
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";
}
}
有什么帮助吗?
你想要渐变吗?如果是, 帮助你
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)
}
和
et4.setBackgroundColor(Color.parseColor(checkdata(f4)));
->
et4.setBackgroundColor(checkdata(f4));
对于对此问题感兴趣的人!
我只使用了 RGB(HEX) 的蓝色(带字符串分隔)并将 B 的十六进制转换为整数。然后我用了梯度formula。在这个浮点值之后,我四舍五入为整数并转换为十六进制值。整个 Hex -> Int -> Float -> Int -> Hex 转换已在 Blue 上完成 only.Now 我将 B 连接到 RG 并且我根据变量值更新了 RGB。
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;
}
这对我有用!谢谢。评论如有问题
我正在尝试以编程方式将 1.0 到 5.0 浮点值映射到 RGB 颜色。下面的程序对我来说工作正常,但它采用浮点值并与一些范围和 returns 硬编码 RGB 值进行比较。
相反,我想根据浮点值更改 RGB 值。按照我的逻辑,我肯定会得到 1.1 到 1.9 的相同颜色,但我想根据 1.0 到 5.0 动态更改 RGB。
如果我尝试移动 R、G、B,但无法做到。
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";
}
}
有什么帮助吗?
你想要渐变吗?如果是, 帮助你
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)
}
和
et4.setBackgroundColor(Color.parseColor(checkdata(f4)));
->
et4.setBackgroundColor(checkdata(f4));
对于对此问题感兴趣的人!
我只使用了 RGB(HEX) 的蓝色(带字符串分隔)并将 B 的十六进制转换为整数。然后我用了梯度formula。在这个浮点值之后,我四舍五入为整数并转换为十六进制值。整个 Hex -> Int -> Float -> Int -> Hex 转换已在 Blue 上完成 only.Now 我将 B 连接到 RG 并且我根据变量值更新了 RGB。
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;
}
这对我有用!谢谢。评论如有问题