hoshi-takanori さんがコメントされました URL 先のコードをenum化して試してみましたが… 黒(rgb=0) を指定しても黒にならず orz ←バグでした。(Color.red()等が0~255ではありませんでした)
NamedColor.getApproximateColor を弄ってみてください・・・なレベルになってしまいました。
NamedColor.java
java
1import android.graphics.Color;
2
3enum NamedColor {
4 AliceBlue("AliceBlue", 0xF0F8FF), AntiqueWhite("AntiqueWhite", 0xFAEBD7), Aqua("Aqua", 0x00FFFF), Aquamarine("Aquamarine", 0x7FFFD4), Azure("Azure", 0xF0FFFF),
5 Beige("Beige", 0xF5F5DC), Bisque("Bisque", 0xFFE4C4), Black("Black", 0x000000), BlanchedAlmond("BlanchedAlmond", 0xFFEBCD), Blue("Blue", 0x0000FF), BlueViolet("BlueViolet", 0x8A2BE2), Brown("Brown", 0xA52A2A), BurlyWood("BurlyWood", 0xDEB887),
6 CadetBlue("CadetBlue", 0x5F9EA0), Chartreuse("Chartreuse", 0x7FFF00), Chocolate("Chocolate", 0xD2691E), Coral("Coral", 0xFF7F50), CornflowerBlue("CornflowerBlue", 0x6495ED), Cornsilk("Cornsilk", 0xFFF8DC), Crimson("Crimson", 0xDC143C), Cyan("Cyan", 0x00FFFF),
7 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),
8 Firebrick("Firebrick", 0xB22222), FloralWhite("FloralWhite", 0xFFFAF0), ForestGreen("ForestGreen", 0x228B22), Fuchsia("Fuchsia", 0xFF00FF),
9 Gainsboro("Gainsboro", 0xDCDCDC), GhostWhite("GhostWhite", 0xF8F8FF), Gold("Gold", 0xFFD700), Goldenrod("Goldenrod", 0xDAA520), Gray("Gray", 0x808080), Green("Green", 0x008000), GreenYellow("GreenYellow", 0xADFF2F),
10 Honeydew("Honeydew", 0xF0FFF0), HotPink("HotPink", 0xFF69B4),
11 IndianRed("IndianRed", 0xCD5C5C), Indigo("Indigo", 0x4B0082), Ivory("Ivory", 0xFFFFF0),
12 Khaki("Khaki", 0xF0E68C),
13 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),
14 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),
15 NavajoWhite("NavajoWhite", 0xFFDEAD), Navy("Navy", 0x000080),
16 OldLace("OldLace", 0xFDF5E6), Olive("Olive", 0x808000), OliveDrab("OliveDrab", 0x6B8E23), Orange("Orange", 0xFFA500), OrangeRed("OrangeRed", 0xFF4500), Orchid("Orchid", 0xDA70D6),
17 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),
18 Red("Red", 0xFF0000), RosyBrown("RosyBrown", 0xBC8F8F), RoyalBlue("RoyalBlue", 0x4169E1),
19 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),
20 Tan("Tan", 0xD2B48C), Teal("Teal", 0x008080), Thistle("Thistle", 0xD8BFD8), Tomato("Tomato", 0xFF6347), Turquoise("Turquoise", 0x40E0D0),
21 Violet("Violet", 0xEE82EE),
22 Wheat("Wheat", 0xF5DEB3), White("White", 0xFFFFFF), WhiteSmoke("WhiteSmoke", 0xF5F5F5),
23 Yellow("Yellow", 0xFFFF01), YellowGreen("YellowGreen", 0x9ACD32);
24
25 public static NamedColor getApproximateColor(int rgb) {
26 int min = Integer.MAX_VALUE;
27 int r = Color.red(rgb);
28 int g = Color.green(rgb);
29 int b = Color.blue(rgb);
30 NamedColor color = null;
31 for(NamedColor c : values()) {
32 int mse = c.computeMSE(r, g, b);
33 if(min > mse) {
34 min = mse;
35 color = c;
36 }
37 }
38 return color;
39 }
40
41 private String name;
42 private int r, g, b;
43
44 NamedColor(String name, int rgb) {
45 this.name = name;
46 r = Color.red(rgb);
47 g = Color.green(rgb);
48 b = Color.blue(rgb);
49 }
50
51 public String getName() { return name; }
52 public Color getColor() { return Color.valueOf(Color.rgb(r, g, b)); }
53 public String toString() {
54 StringBuffer sb = new StringBuffer(super.toString());
55 sb.append("[r=").append(r);
56 sb.append(",g=").append(g);
57 sb.append(",b=").append(b);
58 return sb.append("]").toString();
59 }
60
61 private int computeMSE(int pr, int pg, int pb) {
62 return (int)(((pr - r) * (pr - r) + (pg - g) * (pg - g) + (pb - b) * (pb - b)) / 3);
63 }
64}
MainActivity.java
java
1import androidx.appcompat.app.AppCompatActivity;
2
3import android.os.Bundle;
4import android.text.*;
5import android.widget.*;
6
7public class MainActivity extends AppCompatActivity {
8 @Override
9 protected void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.activity_main);
12
13 EditText input = findViewById(R.id.input);
14 TextView output = findViewById(R.id.output);
15 input.addTextChangedListener(new TextWatcher() {
16 @Override
17 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
18 @Override
19 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
20 @Override
21 public void afterTextChanged(Editable editable) {
22 try {
23 int rgb = Integer.decode("0x" + editable.toString());
24 NamedColor nc = NamedColor.getApproximateColor(rgb);
25 output.setText(nc == null ? "(不明)" : nc.getName());
26 } catch(NumberFormatException e) {
27 output.setText("(エラー)");
28 }
29 }
30 });
31 }
32}
レイアウト:
activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<RelativeLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:app="http://schemas.android.com/apk/res-auto"
5 xmlns:tools="http://schemas.android.com/tools"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 tools:context=".MainActivity">
9
10 <EditText
11 android:id="@+id/input"
12 android:layout_width="match_parent"
13 android:layout_height="wrap_content"
14 android:hint="RGB(16進数6桁)"
15 android:inputType="text"
16 android:digits="0123456789abcdef"
17 android:maxLength="6"
18 android:layout_centerInParent="true" />
19
20 <TextView
21 android:id="@+id/output"
22 android:layout_width="match_parent"
23 android:layout_height="wrap_content"
24 android:text=""
25 android:layout_below="@id/input"
26 android:layout_marginTop="20dp"
27 android:layout_centerHorizontal="true" />
28
29</RelativeLayout>