File: Color.java

package info (click to toggle)
libcaca 0.99.beta19-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,200 kB
  • ctags: 3,824
  • sloc: ansic: 22,346; python: 2,316; cs: 1,213; cpp: 1,114; java: 916; objc: 836; makefile: 606; sh: 457; ruby: 193; asm: 65
file content (82 lines) | stat: -rw-r--r-- 2,346 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
 *  libcaca       Java bindings for libcaca
 *  Copyright (c) 2009 Adrien Grand <jpountz@dinauz.org>
 *
 *  This library is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What the Fuck You Want
 *  to Public License, Version 2, as published by Sam Hocevar. See
 *  http://www.wtfpl.net/ for more details.
 */

package org.zoy.caca;

public abstract class Color {

  public static enum Ansi {
    BLACK((byte)0x00),
    BLUE((byte)0x01),
    GREEN((byte)0x02),
    CYAN((byte)0x03),
    RED((byte)0x04),
    MAGENTA((byte)0x05),
    BROWN((byte)0x06),
    LIGHTGREY((byte)0x07),
    DARKGREY((byte)0x08),
    LIGHTBLUE((byte)0x09),
    LIGHTGREEN((byte)0x0a),
    LIGHTCYAN((byte)0x0b),
    LIGHTRED((byte)0x0c),
    LIGHTMAGENTA((byte)0x0d),
    YELLOW((byte)0x0e),
    WHITE((byte)0x0f),
    DEFAULT((byte)0x10),
    TRANSPARENT((byte)0x20);


    private Ansi(byte code) {
      this.code = code;
    }
    protected byte code;

    public static Ansi forCode(byte code) {
      Ansi[] values = Ansi.values();
      for (Ansi color : values) {
        if (color.code == code) {
          return color;
        }
      }
      return null;
    }
  }

  // Use ints mostly because it is more convenient (no need to cast)
  public static class Argb {
    public Argb(int code) {
      if (code < 0 || code > Short.MAX_VALUE - Short.MIN_VALUE) {
        throw new IllegalArgumentException("Code should be a 16-bit unsigned integer");
      }
      this.code = (short)code;
    }
    public Argb(int alpha, int red, int green, int blue) {
      if (alpha < 0 || alpha >15) {
        throw new IllegalArgumentException("alpha should be a 4-bit unsigned integer");
      }
      if (red < 0 || red >15) {
        throw new IllegalArgumentException("red should be a 4-bit unsigned integer");
      }
      if (green < 0 || green >15) {
        throw new IllegalArgumentException("green should be a 4-bit unsigned integer");
      }
      if (blue < 0 || blue >15) {
        throw new IllegalArgumentException("blue should be a 4-bit unsigned integer");
      }
      this.code = (short)((alpha << 16) + (red << 8) + (green << 4) + blue);
    }
    public short getCode() {
      return code;
    }
    protected short code;
  }

}