fork(5) download
  1. import java.awt.Color;
  2. import java.lang.reflect.Field;
  3.  
  4. public class Main {
  5.  
  6. public static String getColorName(Color c) {
  7. for (Field f : Color.class.getFields()) {
  8. try {
  9. if (f.getType() == Color.class && f.get(null).equals(c)) {
  10. return f.getName();
  11. }
  12. } catch (java.lang.IllegalAccessException e) {
  13. // it should never get to here
  14. }
  15. }
  16. return "unknown";
  17. }
  18.  
  19. public static void main(String[] args) {
  20.  
  21. System.out.println(getColorName(Color.BLACK));
  22. System.out.println(getColorName(Color.BLUE));
  23. System.out.println(getColorName(new Color(1,2,3)));
  24.  
  25. }
  26.  
  27. }
Success #stdin #stdout 0.04s 246656KB
stdin
Standard input is empty
stdout
black
blue
unknown