| 1 |
package uk.me.parabola.imgfmt.app.typ; |
| 2 |
|
| 3 |
import uk.me.parabola.imgfmt.FormatException; |
| 4 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 5 |
|
| 6 |
public class Rgb { |
| 7 |
private final int b; |
| 8 |
private final int g; |
| 9 |
private final int r; |
| 10 |
private final int a; |
| 11 |
|
| 12 |
public Rgb(int r, int g, int b, int a) { |
| 13 |
this.r = r; |
| 14 |
this.g = g; |
| 15 |
this.b = b; |
| 16 |
this.a = a; |
| 17 |
} |
| 18 |
|
| 19 |
public Rgb(int r, int g, int b) { |
| 20 |
this(r, g, b, 0xff); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public Rgb(String in) { |
| 33 |
String colour = in; |
| 34 |
if (colour.startsWith("#")) |
| 35 |
colour = colour.substring(1); |
| 36 |
|
| 37 |
r = Integer.parseInt(colour.substring(0, 2), 16); |
| 38 |
g = Integer.parseInt(colour.substring(2, 4), 16); |
| 39 |
b = Integer.parseInt(colour.substring(4, 6), 16); |
| 40 |
if (colour.length() > 6) |
| 41 |
a = Integer.parseInt(colour.substring(6, 8), 16); |
| 42 |
else |
| 43 |
a = 0xff; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public Rgb(Rgb rgb, int alpha) { |
| 50 |
this(rgb.r, rgb.g, rgb.b, alpha); |
| 51 |
} |
| 52 |
|
| 53 |
public void write(ImgFileWriter writer, byte type) { |
| 54 |
if (type != 0x10) |
| 55 |
throw new FormatException("Invalid color deep"); |
| 56 |
writer.put1u(b); |
| 57 |
writer.put1u(g); |
| 58 |
writer.put1u(r); |
| 59 |
} |
| 60 |
|
| 61 |
public boolean isTransparent() { |
| 62 |
return a == 0; |
| 63 |
} |
| 64 |
|
| 65 |
public String toString() { |
| 66 |
if (a == 0xff) |
| 67 |
return String.format("#%02x%02x%02x", r, g, b); |
| 68 |
else |
| 69 |
return String.format("#%02x%02x%02x%02x", r, g, b, a); |
| 70 |
} |
| 71 |
|
| 72 |
public int getB() { |
| 73 |
return b; |
| 74 |
} |
| 75 |
|
| 76 |
public int getG() { |
| 77 |
return g; |
| 78 |
} |
| 79 |
|
| 80 |
public int getR() { |
| 81 |
return r; |
| 82 |
} |
| 83 |
|
| 84 |
public int getA() { |
| 85 |
return a; |
| 86 |
} |
| 87 |
} |