teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

6

コード修正

2020/02/01 08:37

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -16,29 +16,29 @@
16
16
  yellow, red, green, blue, cantRead, outOfRange;
17
17
 
18
18
  public static ColorThing from(Color color) {
19
- if (in_range(color, 0.2, 0.4, 0.45, 1.01, 0.0, 0.2)) {
19
+ if (inRange(color, 0.2, 0.4, 0.45, 1.01, 0.0, 0.2)) {
20
20
  return yellow;
21
- } else if (in_range(color, 0.3, 1.01, 0.2, 0.48, 0.0, 0.3)) {
21
+ } else if (inRange(color, 0.3, 1.01, 0.2, 0.48, 0.0, 0.3)) {
22
22
  return red;
23
- } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.2, 0.27)) {
23
+ } else if (inRange(color, 0.0, 0.25, 0.4, 1.01, 0.2, 0.27)) {
24
24
  return green;
25
- } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.27, 1.01)) {
25
+ } else if (inRange(color, 0.0, 0.25, 0.4, 1.01, 0.27, 1.01)) {
26
26
  return blue;
27
27
  } else {
28
28
  return cantRead;
29
29
  }
30
30
  }
31
31
 
32
- private static boolean in_range(Color color,
32
+ private static boolean inRange(Color color,
33
33
  double redMin, double redOver,
34
34
  double greenMin, double greenOver,
35
35
  double blueMin, double blueOver) {
36
- return in_range(color.red, redMin, redOver)
36
+ return inRange(color.red, redMin, redOver)
37
- && in_range(color.green, greenMin, greenOver)
37
+ && inRange(color.green, greenMin, greenOver)
38
- && in_range(color.blue, blueMin, blueOver);
38
+ && inRange(color.blue, blueMin, blueOver);
39
39
  }
40
40
 
41
- private static boolean in_range(double color, double min, double over) {
41
+ private static boolean inRange(double color, double min, double over) {
42
42
  return min <= color && color < over;
43
43
  }
44
44
  };

5

コメント削除

2020/02/01 08:37

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -17,19 +17,14 @@
17
17
 
18
18
  public static ColorThing from(Color color) {
19
19
  if (in_range(color, 0.2, 0.4, 0.45, 1.01, 0.0, 0.2)) {
20
- // yellow actions
21
20
  return yellow;
22
21
  } else if (in_range(color, 0.3, 1.01, 0.2, 0.48, 0.0, 0.3)) {
23
- // red actions
24
22
  return red;
25
23
  } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.2, 0.27)) {
26
- // green actions
27
24
  return green;
28
25
  } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.27, 1.01)) {
29
- // blue actions
30
26
  return blue;
31
27
  } else {
32
- // actions when unreadable
33
28
  return cantRead;
34
29
  }
35
30
  }

4

コード変更

2020/02/01 08:33

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -15,19 +15,6 @@
15
15
  enum ColorThing {
16
16
  yellow, red, green, blue, cantRead, outOfRange;
17
17
 
18
- public static boolean in_range(double color, double min, double over) {
19
- return min <= color && color < over;
20
- }
21
-
22
- public static boolean in_range(Color color,
23
- double redMin, double redOver,
24
- double greenMin, double greenOver,
25
- double blueMin, double blueOver) {
26
- return in_range(color.red, redMin, redOver)
27
- && in_range(color.green, greenMin, greenOver)
28
- && in_range(color.blue, blueMin, blueOver);
29
- }
30
-
31
18
  public static ColorThing from(Color color) {
32
19
  if (in_range(color, 0.2, 0.4, 0.45, 1.01, 0.0, 0.2)) {
33
20
  // yellow actions
@@ -46,6 +33,19 @@
46
33
  return cantRead;
47
34
  }
48
35
  }
36
+
37
+ private static boolean in_range(Color color,
38
+ double redMin, double redOver,
39
+ double greenMin, double greenOver,
40
+ double blueMin, double blueOver) {
41
+ return in_range(color.red, redMin, redOver)
42
+ && in_range(color.green, greenMin, greenOver)
43
+ && in_range(color.blue, blueMin, blueOver);
44
+ }
45
+
46
+ private static boolean in_range(double color, double min, double over) {
47
+ return min <= color && color < over;
48
+ }
49
49
  };
50
50
 
51
51
  public class Main {

3

文言訂正

2020/02/01 08:31

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -1,5 +1,5 @@
1
- 比較メソッドにしてはいかがですか?
1
+ メソッドにしてはいかがですか?
2
- detectColor クラスにメソッド追加できるのが一番でょうけど、とりあえずローカルメソッドで
2
+ ColorThingのメソッドてみました
3
3
 
4
4
  ```java
5
5
  class Color {
@@ -56,6 +56,4 @@
56
56
  System.out.println(colorThing);
57
57
  }
58
58
  }
59
- ```
59
+ ```
60
-
61
- さらに、色の範囲と対応enumのリストにしてループで回すのもいいかもしれません。

2

コード修正

2020/02/01 08:25

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -31,19 +31,19 @@
31
31
  public static ColorThing from(Color color) {
32
32
  if (in_range(color, 0.2, 0.4, 0.45, 1.01, 0.0, 0.2)) {
33
33
  // yellow actions
34
- return ColorThing.yellow;
34
+ return yellow;
35
35
  } else if (in_range(color, 0.3, 1.01, 0.2, 0.48, 0.0, 0.3)) {
36
36
  // red actions
37
- return ColorThing.red;
37
+ return red;
38
38
  } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.2, 0.27)) {
39
39
  // green actions
40
- return ColorThing.green;
40
+ return green;
41
41
  } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.27, 1.01)) {
42
42
  // blue actions
43
- return ColorThing.blue;
43
+ return blue;
44
44
  } else {
45
45
  // actions when unreadable
46
- return ColorThing.cantRead;
46
+ return cantRead;
47
47
  }
48
48
  }
49
49
  };

1

コード修正

2020/02/01 08:21

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -2,34 +2,60 @@
2
2
  detectColor の クラスにメソッド追加できるのが一番でしょうけど、とりあえずローカルメソッドで。
3
3
 
4
4
  ```java
5
+ class Color {
6
+ double red, green, blue;
7
+
8
+ Color(double red, double green, double blue) {
9
+ this.red = red;
10
+ this.green = green;
11
+ this.blue = blue;
12
+ }
13
+ }
14
+
15
+ enum ColorThing {
16
+ yellow, red, green, blue, cantRead, outOfRange;
17
+
18
+ public static boolean in_range(double color, double min, double over) {
19
+ return min <= color && color < over;
20
+ }
21
+
5
22
  public static boolean in_range(Color color,
6
- float redMin, float redOver,
23
+ double redMin, double redOver,
7
- float greenMin, float greemOver,
24
+ double greenMin, double greenOver,
8
- float blueMin, float blueOver) {
25
+ double blueMin, double blueOver) {
9
- return redMin <= color.red && color.red < redOver
26
+ return in_range(color.red, redMin, redOver)
10
- && greenMin <= color.green && color.green < greenOver
27
+ && in_range(color.green, greenMin, greenOver)
11
- && blueMin <= color.blue && color.blue < blueOver;
28
+ && in_range(color.blue, blueMin, blueOver);
12
29
  }
13
- ```
14
30
 
15
- ```java
16
- ColorThing colorOutput = ColorThing.cantRead;
31
+ public static ColorThing from(Color color) {
17
- if (in_range(detectedColor, 0.2, 0.4, 0.45, 1.01, 0.0, 0.2)) {
32
+ if (in_range(color, 0.2, 0.4, 0.45, 1.01, 0.0, 0.2)) {
18
33
  // yellow actions
19
- colorOutput = ColorThing.yellow;
34
+ return ColorThing.yellow;
20
- } else if (in_range(detectedColor, 0.3, 1.01, 0.2, 0.48, 0.0, 0.3)) {
35
+ } else if (in_range(color, 0.3, 1.01, 0.2, 0.48, 0.0, 0.3)) {
21
36
  // red actions
22
- colorOutput = ColorThing.red;
37
+ return ColorThing.red;
23
- } else if (in_range(detectedColor, 0.0, 0.25, 0.4, 1.01, 0.2, 0.27)) {
38
+ } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.2, 0.27)) {
24
39
  // green actions
25
- colorOutput = ColorThing.green;
40
+ return ColorThing.green;
26
- } else if (in_range(detectedColor, 0.0, 0.25, 0.4, 1.01, 0.27, 1.01)) {
41
+ } else if (in_range(color, 0.0, 0.25, 0.4, 1.01, 0.27, 1.01)) {
27
42
  // blue actions
28
- colorOutput = ColorThing.blue;
43
+ return ColorThing.blue;
29
44
  } else {
30
45
  // actions when unreadable
31
- colorOutput = ColorThing.cantRead;
46
+ return ColorThing.cantRead;
32
47
  }
48
+ }
49
+ };
50
+
51
+ public class Main {
52
+
53
+ public static void main(String[] args) {
54
+ Color detectColor = new Color(0.2, 0.5, 0.7);
55
+ ColorThing colorThing = ColorThing.from(detectColor);
56
+ System.out.println(colorThing);
57
+ }
58
+ }
33
59
  ```
34
60
 
35
61
  さらに、色の範囲と対応enumのリストにしてループで回すのもいいかもしれません。