回答編集履歴
3
package 削除
test
CHANGED
@@ -10,10 +10,6 @@
|
|
10
10
|
|
11
11
|
```java
|
12
12
|
|
13
|
-
package com.teratail.q351712;
|
14
|
-
|
15
|
-
|
16
|
-
|
17
13
|
import android.graphics.Color;
|
18
14
|
|
19
15
|
|
@@ -148,10 +144,6 @@
|
|
148
144
|
|
149
145
|
```java
|
150
146
|
|
151
|
-
package com.teratail.q351712;
|
152
|
-
|
153
|
-
|
154
|
-
|
155
147
|
import androidx.appcompat.app.AppCompatActivity;
|
156
148
|
|
157
149
|
|
2
コードフォーマット微修正
test
CHANGED
@@ -114,11 +114,7 @@
|
|
114
114
|
|
115
115
|
|
116
116
|
|
117
|
-
public String getName() {
|
117
|
+
public String getName() { return name; }
|
118
|
-
|
119
|
-
return name;
|
120
|
-
|
121
|
-
}
|
122
118
|
|
123
119
|
public Color getColor() { return Color.valueOf(Color.rgb(r, g, b)); }
|
124
120
|
|
1
NamedColor バグ修正
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
hoshi-takanori さんがコメントされました URL 先のコードをenum化して試してみましたが… 黒(rgb=0) を指定しても黒にならず orz
|
1
|
+
hoshi-takanori さんがコメントされました URL 先のコードをenum化して試してみましたが… ~~黒(rgb=0) を指定しても黒にならず orz~~ ←バグでした。(Color.red()等が0~255ではありませんでした)
|
2
2
|
|
3
3
|
|
4
4
|
|
@@ -20,57 +20,57 @@
|
|
20
20
|
|
21
21
|
enum NamedColor {
|
22
22
|
|
23
|
-
AliceBlue("AliceBlue", 0xF
|
24
|
-
|
25
|
-
Beige("Beige", 0xF
|
26
|
-
|
27
|
-
CadetBlue("CadetBlue", 0x
|
28
|
-
|
29
|
-
DarkBlue("DarkBlue", 0x
|
30
|
-
|
31
|
-
Firebrick("Firebrick", 0x
|
32
|
-
|
33
|
-
Gainsboro("Gainsboro", 0x
|
34
|
-
|
35
|
-
Honeydew("Honeydew", 0xF
|
36
|
-
|
37
|
-
IndianRed("IndianRed", 0x
|
38
|
-
|
39
|
-
Khaki("Khaki", 0xF
|
40
|
-
|
41
|
-
Lavender("Lavender", 0x
|
42
|
-
|
43
|
-
Magenta("Magenta", 0xFF
|
44
|
-
|
45
|
-
NavajoWhite("NavajoWhite", 0xFF
|
46
|
-
|
47
|
-
OldLace("OldLace", 0xF
|
48
|
-
|
49
|
-
PaleGoldenrod("PaleGoldenrod", 0x
|
50
|
-
|
51
|
-
Red("Red", 0xFF
|
52
|
-
|
53
|
-
SaddleBrown("SaddleBrown", 0x
|
54
|
-
|
55
|
-
Tan("Tan", 0x
|
56
|
-
|
57
|
-
Violet("Violet", 0x
|
58
|
-
|
59
|
-
Wheat("Wheat", 0xF
|
60
|
-
|
61
|
-
Yellow("Yellow", 0xFFFF
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
public static NamedColor getApproximateColor(int
|
23
|
+
AliceBlue("AliceBlue", 0xF0F8FF), AntiqueWhite("AntiqueWhite", 0xFAEBD7), Aqua("Aqua", 0x00FFFF), Aquamarine("Aquamarine", 0x7FFFD4), Azure("Azure", 0xF0FFFF),
|
24
|
+
|
25
|
+
Beige("Beige", 0xF5F5DC), Bisque("Bisque", 0xFFE4C4), Black("Black", 0x000000), BlanchedAlmond("BlanchedAlmond", 0xFFEBCD), Blue("Blue", 0x0000FF), BlueViolet("BlueViolet", 0x8A2BE2), Brown("Brown", 0xA52A2A), BurlyWood("BurlyWood", 0xDEB887),
|
26
|
+
|
27
|
+
CadetBlue("CadetBlue", 0x5F9EA0), Chartreuse("Chartreuse", 0x7FFF00), Chocolate("Chocolate", 0xD2691E), Coral("Coral", 0xFF7F50), CornflowerBlue("CornflowerBlue", 0x6495ED), Cornsilk("Cornsilk", 0xFFF8DC), Crimson("Crimson", 0xDC143C), Cyan("Cyan", 0x00FFFF),
|
28
|
+
|
29
|
+
DarkBlue("DarkBlue", 0x00008B), DarkCyan("DarkCyan", 0x008B8B), DarkGoldenrod("DarkGoldenrod", 0xB8860B), DarkGray("DarkGray", 0xA9A9A9), DarkGreen("DarkGreen", 0x006400), DarkKhaki("DarkKhaki", 0xBDB76B), DarkMagenta("DarkMagenta", 0x8B008B), DarkOliveGreen("DarkOliveGreen", 0x556B2F), DarkOrange("DarkOrange", 0xFF8C00), DarkOrchid("DarkOrchid", 0x9932CC), DarkRed("DarkRed", 0x8B0000), DarkSalmon("DarkSalmon", 0xE9967A), DarkSeaGreen("DarkSeaGreen", 0x8FBC8F), DarkSlateBlue("DarkSlateBlue", 0x483D8B), DarkSlateGray("DarkSlateGray", 0x2F4F4F), DarkTurquoise("DarkTurquoise", 0x00CED1), DarkViolet("DarkViolet", 0x9400D3), DeepPink("DeepPink", 0xFF1493), DeepSkyBlue("DeepSkyBlue", 0x00BFFF), DimGray("DimGray", 0x696969), DodgerBlue("DodgerBlue", 0x1E90FF),
|
30
|
+
|
31
|
+
Firebrick("Firebrick", 0xB22222), FloralWhite("FloralWhite", 0xFFFAF0), ForestGreen("ForestGreen", 0x228B22), Fuchsia("Fuchsia", 0xFF00FF),
|
32
|
+
|
33
|
+
Gainsboro("Gainsboro", 0xDCDCDC), GhostWhite("GhostWhite", 0xF8F8FF), Gold("Gold", 0xFFD700), Goldenrod("Goldenrod", 0xDAA520), Gray("Gray", 0x808080), Green("Green", 0x008000), GreenYellow("GreenYellow", 0xADFF2F),
|
34
|
+
|
35
|
+
Honeydew("Honeydew", 0xF0FFF0), HotPink("HotPink", 0xFF69B4),
|
36
|
+
|
37
|
+
IndianRed("IndianRed", 0xCD5C5C), Indigo("Indigo", 0x4B0082), Ivory("Ivory", 0xFFFFF0),
|
38
|
+
|
39
|
+
Khaki("Khaki", 0xF0E68C),
|
40
|
+
|
41
|
+
Lavender("Lavender", 0xE6E6FA), LavenderBlush("LavenderBlush", 0xFFF0F5), LawnGreen("LawnGreen", 0x7CFC00), LemonChiffon("LemonChiffon", 0xFFFACD), LightBlue("LightBlue", 0xADD8E6), LightCoral("LightCoral", 0xF08080), LightCyan("LightCyan", 0xE0FFFF), LightGoldenrodYellow("LightGoldenrodYellow", 0xFAFAD2), LightGray("LightGray", 0xD3D3D3), LightGreen("LightGreen", 0x90EE90), LightPink("LightPink", 0xFFB6C1), LightSalmon("LightSalmon", 0xFFA07A), LightSeaGreen("LightSeaGreen", 0x20B2AA), LightSkyBlue("LightSkyBlue", 0x87CEFA), LightSlateGray("LightSlateGray", 0x778899), LightSteelBlue("LightSteelBlue", 0xB0C4DE), LightYellow("LightYellow", 0xFFFFE0), Lime("Lime", 0x00FF00), LimeGreen("LimeGreen", 0x32CD32), Linen("Linen", 0xFAF0E6),
|
42
|
+
|
43
|
+
Magenta("Magenta", 0xFF00FF), Maroon("Maroon", 0x800000), MediumAquamarine("MediumAquamarine", 0x66CDAA), MediumBlue("MediumBlue", 0x0000CD), MediumOrchid("MediumOrchid", 0xBA55D3), MediumPurple("MediumPurple", 0x9370DB), MediumSeaGreen("MediumSeaGreen", 0x3CB371), MediumSlateBlue("MediumSlateBlue", 0x7B68EE), MediumSpringGreen("MediumSpringGreen", 0x00FA9A), MediumTurquoise("MediumTurquoise", 0x48D1CC), MediumVioletRed("MediumVioletRed", 0xC71585), MidnightBlue("MidnightBlue", 0x191970), MintCream("MintCream", 0xF5FFFA), MistyRose("MistyRose", 0xFFE4E1), Moccasin("Moccasin", 0xFFE4B5),
|
44
|
+
|
45
|
+
NavajoWhite("NavajoWhite", 0xFFDEAD), Navy("Navy", 0x000080),
|
46
|
+
|
47
|
+
OldLace("OldLace", 0xFDF5E6), Olive("Olive", 0x808000), OliveDrab("OliveDrab", 0x6B8E23), Orange("Orange", 0xFFA500), OrangeRed("OrangeRed", 0xFF4500), Orchid("Orchid", 0xDA70D6),
|
48
|
+
|
49
|
+
PaleGoldenrod("PaleGoldenrod", 0xEEE8AA), PaleGreen("PaleGreen", 0x98FB98), PaleTurquoise("PaleTurquoise", 0xAFEEEE), PaleVioletRed("PaleVioletRed", 0xDB7093), PapayaWhip("PapayaWhip", 0xFFEFD5), PeachPuff("PeachPuff", 0xFFDAB9), Peru("Peru", 0xCD853F), Pink("Pink", 0xFFC0CB), Plum("Plum", 0xDDA0DD), PowderBlue("PowderBlue", 0xB0E0E6), Purple("Purple", 0x800080),
|
50
|
+
|
51
|
+
Red("Red", 0xFF0000), RosyBrown("RosyBrown", 0xBC8F8F), RoyalBlue("RoyalBlue", 0x4169E1),
|
52
|
+
|
53
|
+
SaddleBrown("SaddleBrown", 0x8B4513), Salmon("Salmon", 0xFA8072), SandyBrown("SandyBrown", 0xF4A460), SeaGreen("SeaGreen", 0x2E8B57), SeaShell("SeaShell", 0xFFF5EE), Sienna("Sienna", 0xA0522D), Silver("Silver", 0xC0C0C0), SkyBlue("SkyBlue", 0x87CEEB), SlateBlue("SlateBlue", 0x6A5ACD), SlateGray("SlateGray", 0x708090), Snow("Snow", 0xFFFAFA), SpringGreen("SpringGreen", 0x00FF7F), SteelBlue("SteelBlue", 0x4682B4),
|
54
|
+
|
55
|
+
Tan("Tan", 0xD2B48C), Teal("Teal", 0x008080), Thistle("Thistle", 0xD8BFD8), Tomato("Tomato", 0xFF6347), Turquoise("Turquoise", 0x40E0D0),
|
56
|
+
|
57
|
+
Violet("Violet", 0xEE82EE),
|
58
|
+
|
59
|
+
Wheat("Wheat", 0xF5DEB3), White("White", 0xFFFFFF), WhiteSmoke("WhiteSmoke", 0xF5F5F5),
|
60
|
+
|
61
|
+
Yellow("Yellow", 0xFFFF01), YellowGreen("YellowGreen", 0x9ACD32);
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
public static NamedColor getApproximateColor(int rgb) {
|
66
66
|
|
67
67
|
int min = Integer.MAX_VALUE;
|
68
68
|
|
69
|
-
int r = Color.red(
|
69
|
+
int r = Color.red(rgb);
|
70
|
-
|
70
|
+
|
71
|
-
int g = Color.green(
|
71
|
+
int g = Color.green(rgb);
|
72
|
-
|
72
|
+
|
73
|
-
int b = Color.blue(
|
73
|
+
int b = Color.blue(rgb);
|
74
74
|
|
75
75
|
NamedColor color = null;
|
76
76
|
|
@@ -96,15 +96,19 @@
|
|
96
96
|
|
97
97
|
private String name;
|
98
98
|
|
99
|
-
private
|
99
|
+
private int r, g, b;
|
100
|
-
|
101
|
-
|
102
|
-
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
-
NamedColor(String name, int
|
103
|
+
NamedColor(String name, int rgb) {
|
104
104
|
|
105
105
|
this.name = name;
|
106
106
|
|
107
|
+
r = Color.red(rgb);
|
108
|
+
|
109
|
+
g = Color.green(rgb);
|
110
|
+
|
107
|
-
|
111
|
+
b = Color.blue(rgb);
|
108
112
|
|
109
113
|
}
|
110
114
|
|
@@ -116,21 +120,27 @@
|
|
116
120
|
|
117
121
|
}
|
118
122
|
|
123
|
+
public Color getColor() { return Color.valueOf(Color.rgb(r, g, b)); }
|
124
|
+
|
119
|
-
public
|
125
|
+
public String toString() {
|
126
|
+
|
120
|
-
|
127
|
+
StringBuffer sb = new StringBuffer(super.toString());
|
128
|
+
|
129
|
+
sb.append("[r=").append(r);
|
130
|
+
|
131
|
+
sb.append(",g=").append(g);
|
132
|
+
|
133
|
+
sb.append(",b=").append(b);
|
134
|
+
|
121
|
-
return
|
135
|
+
return sb.append("]").toString();
|
122
|
-
|
136
|
+
|
123
|
-
}
|
137
|
+
}
|
124
|
-
|
125
|
-
|
126
|
-
|
138
|
+
|
139
|
+
|
140
|
+
|
127
|
-
private int computeMSE(int r, int g, int b) {
|
141
|
+
private int computeMSE(int pr, int pg, int pb) {
|
128
|
-
|
142
|
+
|
129
|
-
return (int)
|
143
|
+
return (int)(((pr - r) * (pr - r) + (pg - g) * (pg - g) + (pb - b) * (pb - b)) / 3);
|
130
|
-
|
131
|
-
+ (g - color.green()) * (g - color.green())
|
132
|
-
|
133
|
-
+ (b - color.blue()) * (b - color.blue())) / 3);
|
134
144
|
|
135
145
|
}
|
136
146
|
|