简体   繁体   中英

Convert a color from the Color class into a human readable String

I wish to convert a Color Object into a human readable String.

To clarify I'm just looking for a conversion of the Class's static variables.

eg.

  1. Color.YELLOW -> "yellow"
  2. Color.RED -> "red"
Color red = Color.RED;
System.out.println(red.conversionMethod());

I want an output of "red"

Thank you in advance for your help.

Get use of Java reflection. This code gives useful output. You can remove unwanted ones from ArrayList via something like list.remove("BITMASK");

import java.awt.Color;
import java.lang.reflect.Field;
import java.util.ArrayList;

public class Test
{
    public static void main ( String [] args )
    {
        Field [] names = Color.class.getFields();
        ArrayList < String > list = new ArrayList < String >();

        for ( Field name: names )
            list.add( name.getName() );
        for ( int i = 0; i < list.size(); i++ )
            System.out.println( list.get( i ) );
    }
}

Output:

white WHITE lightGray LIGHT_GRAY gray GRAY darkGray DARK_GRAY black BLACK red RED pink PINK orange ORANGE yellow YELLOW green GREEN magenta MAGENTA cyan CYAN blue BLUE OPAQUE BITMASK TRANSLUCENT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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