-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathScalarPreference.java
More file actions
58 lines (49 loc) · 1.85 KB
/
ScalarPreference.java
File metadata and controls
58 lines (49 loc) · 1.85 KB
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
package org.hyperonline.hyperlib.pref;
import org.opencv.core.Scalar;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
/**
* {@link ScalarPreference}
*
* @author James
*/
public class ScalarPreference implements Supplier<Scalar> {
private List<DoublePreference> m_prefs = new ArrayList<>();
/**
* Construct a scalar preference with the given name and components.
*
* @param name The name of the preferences object.
* @param components A sequence of letters indicating the components. For example, "RGB", "HSV",
* "XYZ", etc. It may be any length.
* @param defaults Default settings for components
*/
public ScalarPreference(String name, String components, double... defaults) {
if (defaults.length != components.length()) {
throw new IllegalArgumentException("Components and defaults have different lengths");
}
for (int i = 0; i < defaults.length; i++) {
String childName = name + PreferencesSet.SEPARATOR + components.charAt(i);
m_prefs.add(new DoublePreference(childName, defaults[i]));
}
}
/** Construct a scalar preference, while also updating the right info inside the parent set. */
ScalarPreference(PreferencesSet parent, String name, String components, double[] defaults) {
if (defaults.length != components.length()) {
throw new IllegalArgumentException("Components and defaults have different lengths");
}
for (int i = 0; i < defaults.length; i++) {
String childName = name + PreferencesSet.SEPARATOR + components.charAt(i);
m_prefs.add(parent.addDouble(childName, defaults[i]));
}
}
/**
* Get the current value of the preference.
*
* @return The current value of the preference.
*/
@Override
public Scalar get() {
return new Scalar(m_prefs.stream().mapToDouble(DoublePreference::get).toArray());
}
}