Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
ColorParser.java
Go to the documentation of this file.
1 // This file is part of MicropolisJ.
2 // Copyright (C) 2013 Jason Long
3 // Portions Copyright (C) 1989-2007 Electronic Arts Inc.
4 //
5 // MicropolisJ is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GPLv3, with additional terms.
7 // See the README file, included in this distribution, for details.
8 
9 package micropolisj.gui;
10 
11 import java.awt.Color;
12 
13 public class ColorParser
14 {
15  private ColorParser() {}
16 
17  static Color parseColor(String str)
18  {
19  if (str.startsWith("#") && str.length() == 7) {
20  return new Color(Integer.parseInt(str.substring(1), 16));
21  }
22  else if (str.startsWith("rgba(") && str.endsWith(")")) {
23  String [] parts = str.substring(5,str.length()-1).split(",");
24  int r = Integer.parseInt(parts[0]);
25  int g = Integer.parseInt(parts[1]);
26  int b = Integer.parseInt(parts[2]);
27  double aa = Double.parseDouble(parts[3]);
28  int a = Math.min(255, (int)Math.floor(aa*256.0));
29  return new Color(r,g,b,a);
30  }
31  else {
32  throw new Error("invalid color format: "+str);
33  }
34  }
35 }