fr.arakne.utils.value.Colors.color2()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
/*
2
 * This file is part of ArakneUtils.
3
 *
4
 * ArakneUtils is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * ArakneUtils is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with ArakneUtils.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2020 Vincent Quatrevieux
18
 */
19
20
package fr.arakne.utils.value;
21
22
import fr.arakne.utils.value.helper.RandomUtil;
23
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
24
import org.checkerframework.checker.nullness.qual.Nullable;
25
import org.checkerframework.common.value.qual.IntRange;
26
import org.checkerframework.dataflow.qual.Pure;
27
28
import java.util.Arrays;
29
import java.util.Objects;
30
import java.util.Random;
31
import java.util.stream.Collectors;
32
import java.util.stream.Stream;
33
34
/**
35
 * Colors value type
36
 * The colors are in 24 bits, so the value is in interval [0, 16777216[
37
 *
38
 * Note: This is an immutable value object
39
 */
40
public final class Colors {
41 1
    public static final Colors DEFAULT = new Colors(-1, -1, -1);
42
    public static final int MAX_COLOR_VALUE = 16777215;
43
    private static @MonotonicNonNull RandomUtil random;
44
45
    private final @IntRange(from = -1, to = MAX_COLOR_VALUE) int color1;
46
    private final @IntRange(from = -1, to = MAX_COLOR_VALUE) int color2;
47
    private final @IntRange(from = -1, to = MAX_COLOR_VALUE) int color3;
48
49 1
    public Colors(@IntRange(from = -1, to = MAX_COLOR_VALUE) int color1, @IntRange(from = -1, to = MAX_COLOR_VALUE) int color2, @IntRange(from = -1, to = MAX_COLOR_VALUE) int color3) {
50 1
        this.color1 = color1;
51 1
        this.color2 = color2;
52 1
        this.color3 = color3;
53 1
    }
54
55
    /**
56
     * @return The first sprite color
57
     */
58
    @Pure
59
    public @IntRange(from = -1, to = MAX_COLOR_VALUE) int color1() {
60 1
        return color1;
61
    }
62
63
    /**
64
     * @return The second sprite color
65
     */
66
    @Pure
67
    public @IntRange(from = -1, to = MAX_COLOR_VALUE) int color2() {
68 1
        return color2;
69
    }
70
71
    /**
72
     * @return The third sprite color
73
     */
74
    @Pure
75
    public @IntRange(from = -1, to = MAX_COLOR_VALUE) int color3() {
76 1
        return color3;
77
    }
78
79
    /**
80
     * Get colors into an array
81
     *
82
     * Example:
83
     * <code>
84
     *     new Color(14, 87, 12).toArray(); // int[] {14, 87, 12}
85
     * </code>
86
     *
87
     * @return The array representation of the color
88
     */
89
    @Pure
90
    public @IntRange(from = -1, to = MAX_COLOR_VALUE) int[] toArray() {
91 1
        return new int[] {color1, color2, color3};
92
    }
93
94
    /**
95
     * Get colors as hex array
96
     * If color is default color (i.e. -1), "-1" will be generated
97
     *
98
     * Example:
99
     * <code>
100
     *     new Color(14, 87, 12).toArray(); // int[] {"e", "57", "c"}
101
     * </code>
102
     *
103
     * @return The array representation of the color, in hexadecimal string
104
     */
105
    @Pure
106
    public String[] toHexArray() {
107 1
        return hexStream().toArray(String[]::new);
108
    }
109
110
    /**
111
     * Format the colors to an hexadecimal CSV string
112
     * If color is default color (i.e. -1), "-1" will be generated
113
     *
114
     * Example:
115
     * <code>
116
     *     new Color(14, 87, 12).toHexString(";"); // "e;57;c"
117
     * </code>
118
     *
119
     * @param separator The separator to use between each colors
120
     * @return The hexadecimal CSV string
121
     */
122
    @Pure
123
    public String toHexString(CharSequence separator) {
124 1
        return hexStream().collect(Collectors.joining(separator));
125
    }
126
127
    /**
128
     * Create a new colors set with random values
129
     *
130
     * @return A new random Colors
131
     */
132
    public static Colors randomized() {
133 1
        if (random == null) {
134 1
            random = RandomUtil.createShared();
135
        }
136
137 1
        return randomized(random);
138
    }
139
140
    /**
141
     * Create a new colors set with random values
142
     *
143
     * @param random The random generator
144
     *
145
     * @return A new random Colors
146
     */
147
    @SuppressWarnings("argument") // nextInt bounds are not correctly resolved
148
    public static Colors randomized(Random random) {
149 1
        return new Colors(
150 1
            random.nextInt(MAX_COLOR_VALUE + 1),
151 1
            random.nextInt(MAX_COLOR_VALUE + 1),
152 1
            random.nextInt(MAX_COLOR_VALUE + 1)
153
        );
154
    }
155
156
    @Override
157
    public boolean equals(@Nullable Object o) {
158 1
        if (this == o) {
159 1
            return true;
160
        }
161
162 1
        if (o == null || getClass() != o.getClass()) {
163 1
            return false;
164
        }
165
166 1
        final Colors colors = (Colors) o;
167
168 1
        return color1 == colors.color1 && color2 == colors.color2 && color3 == colors.color3;
169
    }
170
171
    @Override
172
    public int hashCode() {
173 1
        return Objects.hash(color1, color2, color3);
174
    }
175
176
    @Override
177
    public String toString() {
178 1
        return "Colors(" + color1 + ", " + color2 + ", " + color3 + ')';
179
    }
180
181
    @Pure
182
    private Stream<String> hexStream() {
183 1
        return Arrays.stream(toArray()).mapToObj(value -> value == -1 ? "-1" : Integer.toHexString(value));
184
    }
185
}
186