Переглянути джерело

Move Colour to utils.

Change-Id: I73c10f367e5972031d1c9b0871c4cb9043008bec
Reviewed-on: http://gerrit.dmdirc.com/3858
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
changes/58/3858/2
Greg Holmes 9 роки тому
джерело
коміт
e57c5dfc95

+ 124
- 0
src/com/dmdirc/util/colours/Colour.java Переглянути файл

@@ -0,0 +1,124 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util.colours;
24
+
25
+/**
26
+ * A colour represented by an RGB triple. This implementation is immutable.
27
+ */
28
+public class Colour {
29
+
30
+    /** The colour white. */
31
+    public static final Colour WHITE = new Colour(255, 255, 255);
32
+    /** The colour light gray. */
33
+    public static final Colour LIGHT_GRAY = new Colour(192, 192, 192);
34
+    /** The colour gray. */
35
+    public static final Colour GRAY = new Colour(128, 128, 128);
36
+    /** The colour dark gray. */
37
+    public static final Colour DARK_GRAY = new Colour(64, 64, 64);
38
+    /** The colour black. */
39
+    public static final Colour BLACK = new Colour(0, 0, 0);
40
+    /** The colour red. */
41
+    public static final Colour RED = new Colour(255, 0, 0);
42
+    /** The colour pink. */
43
+    public static final Colour PINK = new Colour(255, 175, 175);
44
+    /** The colour orange. */
45
+    public static final Colour ORANGE = new Colour(255, 200, 0);
46
+    /** The colour yellow. */
47
+    public static final Colour YELLOW = new Colour(255, 255, 0);
48
+    /** The colour green. */
49
+    public static final Colour GREEN = new Colour(0, 255, 0);
50
+    /** The colour magenta. */
51
+    public static final Colour MAGENTA = new Colour(255, 0, 255);
52
+    /** The colour cyan. */
53
+    public static final Colour CYAN = new Colour(0, 255, 255);
54
+    /** The colour blue. */
55
+    public static final Colour BLUE = new Colour(0, 0, 255);
56
+    /** The intensity of the red component of this colour (0-255). */
57
+    private final int red;
58
+    /** The intensity of the green component of this colour (0-255). */
59
+    private final int green;
60
+    /** The intensity of the blue component of this colour (0-255). */
61
+    private final int blue;
62
+
63
+    /**
64
+     * Creates a new Colour instance with the given RGB values.
65
+     *
66
+     * @param red   The intensity of the red component of the colour (0-255).
67
+     * @param green The intensity of the green component of the colour (0-255).
68
+     * @param blue  The intensity of the blue component of the colour (0-255).
69
+     */
70
+    public Colour(final int red, final int green, final int blue) {
71
+        this.red = red;
72
+        this.green = green;
73
+        this.blue = blue;
74
+    }
75
+
76
+    /**
77
+     * Gets the intensity of the blue component of this colour.
78
+     *
79
+     * @return The intensity on a scale of 0-255.
80
+     */
81
+    public int getBlue() {
82
+        return blue;
83
+    }
84
+
85
+    /**
86
+     * Gets the intensity of the green component of this colour.
87
+     *
88
+     * @return The intensity on a scale of 0-255.
89
+     */
90
+    public int getGreen() {
91
+        return green;
92
+    }
93
+
94
+    /**
95
+     * Gets the intensity of the red component of this colour.
96
+     *
97
+     * @return The intensity on a scale of 0-255.
98
+     */
99
+    public int getRed() {
100
+        return red;
101
+    }
102
+
103
+    @Override
104
+    public boolean equals(final Object obj) {
105
+        if (obj == null || getClass() != obj.getClass()) {
106
+            return false;
107
+        }
108
+
109
+        final Colour other = (Colour) obj;
110
+
111
+        return this.red == other.getRed() && this.green == other.getGreen()
112
+                && this.blue == other.getBlue();
113
+    }
114
+
115
+    @Override
116
+    public int hashCode() {
117
+        int hash = 7;
118
+        hash = 37 * hash + this.red;
119
+        hash = 37 * hash + this.green;
120
+        hash = 37 * hash + this.blue;
121
+        return hash;
122
+    }
123
+
124
+}

+ 60
- 0
src/com/dmdirc/util/colours/ColourUtils.java Переглянути файл

@@ -0,0 +1,60 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util.colours;
24
+
25
+/**
26
+ * Some util methods for dealing with colours.
27
+ */
28
+public class ColourUtils {
29
+
30
+    private ColourUtils() {
31
+        //Do not instantiate.
32
+    }
33
+
34
+    /**
35
+     * Retrieves the hex representation of the specified colour.
36
+     *
37
+     * @param colour The colour to be parsed
38
+     *
39
+     * @return A 6-digit hex string representing the colour
40
+     */
41
+    public static String getHex(final Colour colour) {
42
+        final int r = colour.getRed();
43
+        final int g = colour.getGreen();
44
+        final int b = colour.getBlue();
45
+
46
+        return toHex(r) + toHex(g) + toHex(b);
47
+    }
48
+
49
+    /**
50
+     * Converts the specified integer (in the range 0-255) into a hex string.
51
+     *
52
+     * @param value The integer to convert
53
+     *
54
+     * @return A 2 char hex string representing the specified integer
55
+     */
56
+    private static String toHex(final int value) {
57
+        final String hex = Integer.toHexString(value);
58
+        return (hex.length() < 2 ? "0" : "") + hex;
59
+    }
60
+}

Завантаження…
Відмінити
Зберегти