Debian Codegraph @

java/org/zoy/caca/Color.java

raw
1 /**
2 * libcaca Java bindings for libcaca
3 * Copyright (c) 2009 Adrien Grand <[email protected]>
4 *
5 * This library is free software. It comes without any warranty, to
6 * the extent permitted by applicable law. You can redistribute it
7 * and/or modify it under the terms of the Do What the Fuck You Want
8 * to Public License, Version 2, as published by Sam Hocevar. See
9 * http://www.wtfpl.net/ for more details.
10 */
11
12 package org.zoy.caca;
13
14 public abstract class Color {
15
16 public static enum Ansi {
17 BLACK((byte)0x00),
18 BLUE((byte)0x01),
19 GREEN((byte)0x02),
20 CYAN((byte)0x03),
21 RED((byte)0x04),
22 MAGENTA((byte)0x05),
23 BROWN((byte)0x06),
24 LIGHTGREY((byte)0x07),
25 DARKGREY((byte)0x08),
26 LIGHTBLUE((byte)0x09),
27 LIGHTGREEN((byte)0x0a),
28 LIGHTCYAN((byte)0x0b),
29 LIGHTRED((byte)0x0c),
30 LIGHTMAGENTA((byte)0x0d),
31 YELLOW((byte)0x0e),
32 WHITE((byte)0x0f),
33 DEFAULT((byte)0x10),
34 TRANSPARENT((byte)0x20);
35
36
37 private Ansi(byte code) {
38 this.code = code;
39 }
40 protected byte code;
41
42 public static Ansi forCode(byte code) {
43 Ansi[] values = Ansi.values();
44 for (Ansi color : values) {
45 if (color.code == code) {
46 return color;
47 }
48 }
49 return null;
50 }
51 }
52
53 // Use ints mostly because it is more convenient (no need to cast)
54 public static class Argb {
55 public Argb(int code) {
56 if (code < 0 || code > Short.MAX_VALUE - Short.MIN_VALUE) {
57 throw new IllegalArgumentException("Code should be a 16-bit unsigned integer");
58 }
59 this.code = (short)code;
60 }
61 public Argb(int alpha, int red, int green, int blue) {
62 if (alpha < 0 || alpha >15) {
63 throw new IllegalArgumentException("alpha should be a 4-bit unsigned integer");
64 }
65 if (red < 0 || red >15) {
66 throw new IllegalArgumentException("red should be a 4-bit unsigned integer");
67 }
68 if (green < 0 || green >15) {
69 throw new IllegalArgumentException("green should be a 4-bit unsigned integer");
70 }
71 if (blue < 0 || blue >15) {
72 throw new IllegalArgumentException("blue should be a 4-bit unsigned integer");
73 }
74 this.code = (short)((alpha << 16) + (red << 8) + (green << 4) + blue);
75 }
76 public short getCode() {
77 return code;
78 }
79 protected short code;
80 }
81
82 }