简体   繁体   English

HTML 颜色文本到 HEX 值

[英]HTML Color text to HEX values

I am working on an Android app, which uses some html data from a website.我正在开发一个 Android 应用程序,它使用来自网站的一些 html 数据。 I have a few pieces of text that are using html colors.我有几段使用 html 颜色的文本。 Like 'red' or 'green'.像“红色”或“绿色”。 Is there any way to convert those strings to HEX values in Java?有没有办法在 Java 中将这些字符串转换为十六进制值?

String hexvalue = Integer.toHexString(Color.parseColor("red")); String hexvalue = Integer.toHexString(Color.parseColor("red"));

//hexvalue is now "ffffff00" //十六进制值现在是“ffffff00”

This will return a color int这将返回一个颜色int

int intColor = android.graphics.Color.parseColor("red") //  -65536

Then you can convert to HEX like so:然后你可以像这样转换为十六进制:

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));

You could easily add the list of HTML colors within your app and translate them.您可以轻松地在应用程序中添加 HTML 颜色列表并进行翻译。 140 color names are defined in the HTML and CSS color specification. HTML 和 CSS 颜色规范中定义了 140 个颜色名称。 The list is here .名单在这里

Given that, it would be trivial to have a HashMap that translates the color names into the appropriate Hex code.鉴于此,拥有一个 HashMap 将颜色名称转换为适当的十六进制代码将是微不足道的。

You could also use Color.parseColor as defined here .您还可以使用Color.parseColor 定义的Color.parseColor That would yield an android color-int, which can be converted to hex like this:这将产生一个 android color-int,它可以像这样转换为十六进制:

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));

If they're using standard CSS 'red' and 'green' then it's equivilent to #FF0000 (rgb(255,0,0)) and #00FF00 (rgb(0,255,0)) respectively.如果他们使用标准 CSS 'red' 和 'green' 那么它分别相当于 #FF0000 (rgb(255,0,0)) 和 #00FF00 (rgb(0,255,0))。

You can also look-up any hex value for a named color in the CSS standard easily at http://www.w3schools.com/cssref/css_colornames.asp您还可以在http://www.w3schools.com/cssref/css_colornames.asp轻松查找 CSS 标准中命名颜色的任何十六进制值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM