Converting a String to Color in Java
Use reflection to access the static member of the Color
class.
Color color;
try {
Field field = Class.forName("java.awt.Color").getField("yellow");
color = (Color)field.get(null);
} catch (Exception e) {
color = null; // Not defined
}
Java - How to convert a Color.toString() into a Color?
Using toString()
"might vary between implementations." Instead save String.valueOf(color.getRGB())
for later reconstruction.
Convert String to Color int
The constant value of Color.Blue
is: -16776961 (0xff0000ff). You are not parsing an int, your are just trying to parse a string and convert it into a int(which won't work).
"Color.BLUE" is not an Integer, but Color.BLUE
will eventually return a constant value.
You need to do this in order to get it right:
int colorInt = Color.BLUE;
views.setTextColor(R.id.tvConfigInput, colorInt);
Edit:
String ColorString = "BLUE";
int colorInt = Color.parseColor(ColorString);
views.setTextColor(R.id.tvConfigInput, colorInt);
How to convert an object of type String rgba(105, 54, 221, 1) to Color in Java-Selenium?
There are more options how to get the color.
Be aware of two different Color data types:org.openqa.selenium.support.Color
vs java.awt.Color
Color color = Color.fromString(cssColorString); // for selenium Color
or
String[] rgba = cssColorString.replace("rgba(", "").replace(")", "").split(", ");
int r = Integer.parseInt(rgba[0]);
int g = Integer.parseInt(rgba[1]);
int b = Integer.parseInt(rgba[2]);
int a = Integer.parseInt(rgba[3]);
Color color2 = new Color(r, g, b, a); // for any of the two classes
Full code example:
package selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.Color;
public class RaduRaspopa extends WebDriverSetup {
public static void main(String[] args) {
WebDriver driver = startChromeDriver(); // wrapped driver init
driver.get("https://www.toolsqa.com/");
WebElement advertisementImage = driver.findElement(By.id("advertisement-image"));
String cssColorString = advertisementImage.getCssValue("color");
Color color = Color.fromString(cssColorString);
System.out.println(color.asHex());
String[] rgba = cssColorString.replace("rgba(", "").replace(")", "").split(", ");
int r = Integer.parseInt(rgba[0]);
int g = Integer.parseInt(rgba[1]);
int b = Integer.parseInt(rgba[2]);
int a = Integer.parseInt(rgba[3]);
Color color2 = new Color(r, g, b, a);
System.out.println(color2.asHex());
driver.quit();
}
}
Output:
Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}) on port 25710
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Led 06, 2022 3:49:31 ODP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
#007bff
#007bff
Java/Android String to Color conversion
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String, int)
For example:
titlebar.setBackgroundColor(Integer.parseInt("545455", 16)+0xFF000000);
processing hex string to color
What I did was simply recreated the color like this:
int c = Integer.parseInt(obj.getString("color"), 16);
c = color(red(c), green(c), blue(c));
Related Topics
Extract Integer Part in String
How to Give System Property to My Test via Gradle and -D
How to Remove Accents from a Unicode String
Hashcode and Equals for Hashset
How to Mix --Class-Path and --Module-Path in Javac (Jdk 9)
Java: Bufferedimage to Byte Array and Back
@Valid Annotation Is Not Validating the List of Child Objects
JPA SQL Server No Dialect Mapping for Jdbc Type: -9
How to Securely Store Encryption Keys in Java
How to Set Color to a Certain Row If Certain Conditions Are Met Using Java
How to Exit a While Loop in Java
Why String.Replaceall() in Java Requires 4 Slashes "\\\\" in Regex to Actually Replace "\"
Process Thymeleaf Variable as HTML Code and Not Text
Java: Global Exception Handler