简体   繁体   English

java.awt.color 到 java.lang.string 转换

[英]java.awt.color to java.lang.string conversion

I would like to convert java.awt.color object to string.我想将 java.awt.color object 转换为字符串。

Color c = new Color(255,0,0);
System.out.print(Color.toString);

It prints java.awt.Color[r=255,g=0,b=0]它打印 java.awt.Color[r=255,g=0,b=0]

Now I suppose to parse this string and decode the color name eg blue etc.现在我想解析这个字符串并解码颜色名称,例如蓝色等。

I was thinking is there any other way I can directly convert this Color into String.我在想有没有其他方法可以直接将此颜色转换为字符串。

Thanks in advance.提前致谢。

EDIT: I would like to store this color values in database.编辑:我想将此颜色值存储在数据库中。 Should I store it as a String or RGB value?我应该将它存储为字符串还是 RGB 值?

No. At least because:不,至少是因为:

  • few rgb combinations have a color name少数 RGB 组合有颜色名称
  • java does not hold the color names in all languages it has locale support for java 不包含其具有区域设置支持的所有语言的颜色名称

But you can do otherwise.但你可以不这样做。 Get the predefined constants in Color and check if any of them has the same RGB as the given color.获取Color中的预定义常量并检查它们中的任何一个是否具有与给定颜色相同的 RGB。 If so - print the name.如果是这样 - 打印名称。

As for your added question - store it as RGB value rather than string.至于您添加的问题 - 将其存储为 RGB 值而不是字符串。

You'd have to start with a list of agreed-upon color names, such as these .您必须从商定的颜色名称列表开始,例如这些. Then build a Map<Color, String>) from which to get the name.然后构建一个Map<Color, String>)从中获取名称。

You can use the methods getRed() , getBlue() , getGreen() , and getAlpha() to get the color's components instead of parsing the string returned from toString() .您可以使用方法getRed()getBlue()getGreen()getAlpha()来获取颜色的组件,而不是解析从toString()返回的字符串。 You can also compare the color with the static fields in java.awt.Color (using Equals .)您还可以将颜色与 java.awt.Color 中的 static 字段进行比较(使用Equals 。)

You could use the HTML4 basic and extends color keywords.您可以使用 HTML4 基本和扩展颜色关键字。 They should be possible to get directly from the rgb values using a table like this: http://www.w3.org/TR/css3-color/#html4它们应该可以使用如下表格直接从 rgb 值中获取: http://www.w3.org/TR/css3-color/#html4

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

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