回答編集履歴

3

package 削除

2021/07/30 07:28

投稿

jimbe
jimbe

スコア13209

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

コードフォーマット微修正

2021/07/30 07:28

投稿

jimbe
jimbe

スコア13209

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 バグ修正

2021/07/30 05:41

投稿

jimbe
jimbe

スコア13209

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", 0xFFF0F8FF), AntiqueWhite("AntiqueWhite", 0xFFFAEBD7), Aqua("Aqua", 0xFF00FFFF), Aquamarine("Aquamarine", 0xFF7FFFD4), Azure("Azure", 0xFFF0FFFF),
24
-
25
- Beige("Beige", 0xFFF5F5DC), Bisque("Bisque", 0xFFFFE4C4), Black("Black", 0xFF000000), BlanchedAlmond("BlanchedAlmond", 0xFFFFEBCD), Blue("Blue", 0xFF0000FF), BlueViolet("BlueViolet", 0xFF8A2BE2), Brown("Brown", 0xFFA52A2A), BurlyWood("BurlyWood", 0xFFDEB887),
26
-
27
- CadetBlue("CadetBlue", 0xFF5F9EA0), Chartreuse("Chartreuse", 0xFF7FFF00), Chocolate("Chocolate", 0xFFD2691E), Coral("Coral", 0xFFFF7F50), CornflowerBlue("CornflowerBlue", 0xFF6495ED), Cornsilk("Cornsilk", 0xFFFFF8DC), Crimson("Crimson", 0xFFDC143C), Cyan("Cyan", 0xFF00FFFF),
28
-
29
- DarkBlue("DarkBlue", 0xFF00008B), DarkCyan("DarkCyan", 0xFF008B8B), DarkGoldenrod("DarkGoldenrod", 0xFFB8860B), DarkGray("DarkGray", 0xFFA9A9A9), DarkGreen("DarkGreen", 0xFF006400), DarkKhaki("DarkKhaki", 0xFFBDB76B), DarkMagenta("DarkMagenta", 0xFF8B008B), DarkOliveGreen("DarkOliveGreen", 0xFF556B2F), DarkOrange("DarkOrange", 0xFFFF8C00), DarkOrchid("DarkOrchid", 0xFF9932CC), DarkRed("DarkRed", 0xFF8B0000), DarkSalmon("DarkSalmon", 0xFFE9967A), DarkSeaGreen("DarkSeaGreen", 0xFF8FBC8F), DarkSlateBlue("DarkSlateBlue", 0xFF483D8B), DarkSlateGray("DarkSlateGray", 0xFF2F4F4F), DarkTurquoise("DarkTurquoise", 0xFF00CED1), DarkViolet("DarkViolet", 0xFF9400D3), DeepPink("DeepPink", 0xFFFF1493), DeepSkyBlue("DeepSkyBlue", 0xFF00BFFF), DimGray("DimGray", 0xFF696969), DodgerBlue("DodgerBlue", 0xFF1E90FF),
30
-
31
- Firebrick("Firebrick", 0xFFB22222), FloralWhite("FloralWhite", 0xFFFFFAF0), ForestGreen("ForestGreen", 0xFF228B22), Fuchsia("Fuchsia", 0xFFFF00FF),
32
-
33
- Gainsboro("Gainsboro", 0xFFDCDCDC), GhostWhite("GhostWhite", 0xFFF8F8FF), Gold("Gold", 0xFFFFD700), Goldenrod("Goldenrod", 0xFFDAA520), Gray("Gray", 0xFF808080), Green("Green", 0xFF008000), GreenYellow("GreenYellow", 0xFFADFF2F),
34
-
35
- Honeydew("Honeydew", 0xFFF0FFF0), HotPink("HotPink", 0xFFFF69B4),
36
-
37
- IndianRed("IndianRed", 0xFFCD5C5C), Indigo("Indigo", 0xFF4B0082), Ivory("Ivory", 0xFFFFFFF0),
38
-
39
- Khaki("Khaki", 0xFFF0E68C),
40
-
41
- Lavender("Lavender", 0xFFE6E6FA), LavenderBlush("LavenderBlush", 0xFFFFF0F5), LawnGreen("LawnGreen", 0xFF7CFC00), LemonChiffon("LemonChiffon", 0xFFFFFACD), LightBlue("LightBlue", 0xFFADD8E6), LightCoral("LightCoral", 0xFFF08080), LightCyan("LightCyan", 0xFFE0FFFF), LightGoldenrodYellow("LightGoldenrodYellow", 0xFFFAFAD2), LightGray("LightGray", 0xFFD3D3D3), LightGreen("LightGreen", 0xFF90EE90), LightPink("LightPink", 0xFFFFB6C1), LightSalmon("LightSalmon", 0xFFFFA07A), LightSeaGreen("LightSeaGreen", 0xFF20B2AA), LightSkyBlue("LightSkyBlue", 0xFF87CEFA), LightSlateGray("LightSlateGray", 0xFF778899), LightSteelBlue("LightSteelBlue", 0xFFB0C4DE), LightYellow("LightYellow", 0xFFFFFFE0), Lime("Lime", 0xFF00FF00), LimeGreen("LimeGreen", 0xFF32CD32), Linen("Linen", 0xFFFAF0E6),
42
-
43
- Magenta("Magenta", 0xFFFF00FF), Maroon("Maroon", 0xFF800000), MediumAquamarine("MediumAquamarine", 0xFF66CDAA), MediumBlue("MediumBlue", 0xFF0000CD), MediumOrchid("MediumOrchid", 0xFFBA55D3), MediumPurple("MediumPurple", 0xFF9370DB), MediumSeaGreen("MediumSeaGreen", 0xFF3CB371), MediumSlateBlue("MediumSlateBlue", 0xFF7B68EE), MediumSpringGreen("MediumSpringGreen", 0xFF00FA9A), MediumTurquoise("MediumTurquoise", 0xFF48D1CC), MediumVioletRed("MediumVioletRed", 0xFFC71585), MidnightBlue("MidnightBlue", 0xFF191970), MintCream("MintCream", 0xFFF5FFFA), MistyRose("MistyRose", 0xFFFFE4E1), Moccasin("Moccasin", 0xFFFFE4B5),
44
-
45
- NavajoWhite("NavajoWhite", 0xFFFFDEAD), Navy("Navy", 0xFF000080),
46
-
47
- OldLace("OldLace", 0xFFFDF5E6), Olive("Olive", 0xFF808000), OliveDrab("OliveDrab", 0xFF6B8E23), Orange("Orange", 0xFFFFA500), OrangeRed("OrangeRed", 0xFFFF4500), Orchid("Orchid", 0xFFDA70D6),
48
-
49
- PaleGoldenrod("PaleGoldenrod", 0xFFEEE8AA), PaleGreen("PaleGreen", 0xFF98FB98), PaleTurquoise("PaleTurquoise", 0xFFAFEEEE), PaleVioletRed("PaleVioletRed", 0xFFDB7093), PapayaWhip("PapayaWhip", 0xFFFFEFD5), PeachPuff("PeachPuff", 0xFFFFDAB9), Peru("Peru", 0xFFCD853F), Pink("Pink", 0xFFFFC0CB), Plum("Plum", 0xFFDDA0DD), PowderBlue("PowderBlue", 0xFFB0E0E6), Purple("Purple", 0xFF800080),
50
-
51
- Red("Red", 0xFFFF0000), RosyBrown("RosyBrown", 0xFFBC8F8F), RoyalBlue("RoyalBlue", 0xFF4169E1),
52
-
53
- SaddleBrown("SaddleBrown", 0xFF8B4513), Salmon("Salmon", 0xFFFA8072), SandyBrown("SandyBrown", 0xFFF4A460), SeaGreen("SeaGreen", 0xFF2E8B57), SeaShell("SeaShell", 0xFFFFF5EE), Sienna("Sienna", 0xFFA0522D), Silver("Silver", 0xFFC0C0C0), SkyBlue("SkyBlue", 0xFF87CEEB), SlateBlue("SlateBlue", 0xFF6A5ACD), SlateGray("SlateGray", 0xFF708090), Snow("Snow", 0xFFFFFAFA), SpringGreen("SpringGreen", 0xFF00FF7F), SteelBlue("SteelBlue", 0xFF4682B4),
54
-
55
- Tan("Tan", 0xFFD2B48C), Teal("Teal", 0xFF008080), Thistle("Thistle", 0xFFD8BFD8), Tomato("Tomato", 0xFFFF6347), Turquoise("Turquoise", 0xFF40E0D0),
56
-
57
- Violet("Violet", 0xFFEE82EE),
58
-
59
- Wheat("Wheat", 0xFFF5DEB3), White("White", 0xFFFFFFFF), WhiteSmoke("WhiteSmoke", 0xFFF5F5F5),
60
-
61
- Yellow("Yellow", 0xFFFFFF01), YellowGreen("YellowGreen", 0xFF9ACD32);
62
-
63
-
64
-
65
- public static NamedColor getApproximateColor(int argb) {
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(argb);
69
+ int r = Color.red(rgb);
70
-
70
+
71
- int g = Color.green(argb);
71
+ int g = Color.green(rgb);
72
-
72
+
73
- int b = Color.blue(argb);
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 Color color;
99
+ private int r, g, b;
100
-
101
-
102
-
100
+
101
+
102
+
103
- NamedColor(String name, int argb) {
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
- color = Color.valueOf(argb);
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 Color getColor() {
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 color;
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) (((r - color.red()) * (r - color.red())
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