回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,135 +1,135 @@
|
|
1
|
-
参考コードがある場合は出典を**質問に明示**してください。
|
2
|
-
[C#:n個の要素からk個を選ぶ組合せを列挙する - Qiita](https://qiita.com/gushwell/items/74a96f56ccb64db3660c)
|
3
|
-
|
4
|
-
> 7色の組み合わせの画像を127通り作りたい
|
5
|
-
|
6
|
-
7個中から1個選ぶ重複しない組合せ:7
|
7
|
-
7個中から2個選ぶ重複しない組合せ:21
|
8
|
-
7個中から3個選ぶ重複しない組合せ:35
|
9
|
-
7個中から4個選ぶ重複しない組合せ:35
|
10
|
-
7個中から5個選ぶ重複しない組合せ:21
|
11
|
-
7個中から6個選ぶ重複しない組合せ:7
|
12
|
-
7個中から7個選ぶ重複しない組合せ:1
|
13
|
-
計:127通りということですね。そしてそれはもうできている。と
|
14
|
-
|
15
|
-
> ファイルが実現したいようにできない、127通りの画像を出力できない。
|
16
|
-
|
17
|
-
`Output_Click`がどういう意図か1ミリもわからないのですが、`canvas.Save`でpngファイルを作るのだから都度ファイル名を変えないといけないのでは?
|
18
|
-
|
19
|
-
> ファイル名を組み合わせた色にあわせるようにdictionaryを使って作れないか摸索
|
20
|
-
|
21
|
-
皆さんおっしゃっていますが、setunさんがどういうファイル名にしたいのかがまったく伝わっていません。
|
22
|
-
|
23
|
-
`ColorDialog`は1個で十分です。使いまわせるのでボタン分作る必要はありません。
|
24
|
-
|
25
|
-
全体的に変数名がひどすぎます。`Button`なのか`Label`なのか`TextBox`なのかまったくわかりません。
|
26
|
-
ひとりで作っているならどうでもいいですが、第三者に見せるのなら、わかりやすい名前に変える・説明をつける・Designer.csを提示する等の配慮が必要です。
|
27
|
-
|
28
|
-
|
29
|
-
やりたいこと自体は分かったので、3色版を書いてみました。
|
30
|
-
```
|
31
|
-
using System;
|
32
|
-
using System.Collections.Generic;
|
33
|
-
using System.Data;
|
34
|
-
using System.Drawing;
|
35
|
-
using System.Drawing.Imaging;
|
36
|
-
using System.Linq;
|
37
|
-
using System.Windows.Forms;
|
38
|
-
|
39
|
-
namespace Questions356810
|
40
|
-
{
|
41
|
-
public partial class Form1 : Form
|
42
|
-
{
|
43
|
-
public Form1()
|
44
|
-
{
|
45
|
-
InitializeComponent();
|
46
|
-
//color1Button.BackColor = Color.Red;
|
47
|
-
//color2Button.BackColor = Color.Lime;
|
48
|
-
//color3Button.BackColor = Color.Blue;
|
49
|
-
}
|
50
|
-
|
51
|
-
private void Color1Button_Click(object sender, EventArgs e)
|
52
|
-
{
|
53
|
-
if (colorDialog1.ShowDialog() == DialogResult.OK)
|
54
|
-
color1Button.BackColor = colorDialog1.Color;
|
55
|
-
}
|
56
|
-
private void Color2Button_Click(object sender, EventArgs e)
|
57
|
-
{
|
58
|
-
if (colorDialog1.ShowDialog() == DialogResult.OK)
|
59
|
-
color2Button.BackColor = colorDialog1.Color;
|
60
|
-
}
|
61
|
-
private void Color3Button_Click(object sender, EventArgs e)
|
62
|
-
{
|
63
|
-
if (colorDialog1.ShowDialog() == DialogResult.OK)
|
64
|
-
color3Button.BackColor = colorDialog1.Color;
|
65
|
-
}
|
66
|
-
|
67
|
-
private void OutputButton_Click(object sender, EventArgs e)
|
68
|
-
{
|
69
|
-
float w = 100;
|
70
|
-
float h = 100;
|
71
|
-
|
72
|
-
using (var canvas = new Bitmap((int)w, (int)h))
|
73
|
-
using (var graphics = Graphics.FromImage(canvas))
|
74
|
-
using (var brush1 = new SolidBrush(color1Button.BackColor))
|
75
|
-
using (var brush2 = new SolidBrush(color2Button.BackColor))
|
76
|
-
using (var brush3 = new SolidBrush(color3Button.BackColor))
|
77
|
-
{
|
78
|
-
// Combination.EnumerateはDictionaryでも使える
|
79
|
-
// 文字列との対応を取りたいのであればこれを渡せばいい
|
80
|
-
var choices = new Dictionary<string, SolidBrush>
|
81
|
-
{
|
82
|
-
{ "Color1", brush1 },
|
83
|
-
{ "Color2", brush2 },
|
84
|
-
{ "Color3", brush3 },
|
85
|
-
};
|
86
|
-
|
87
|
-
var count = 1;
|
88
|
-
for (var i = 1; i <= choices.Count; i++)
|
89
|
-
{
|
90
|
-
var combinations = Combination.Enumerate(choices, i, false);
|
91
|
-
foreach (var combination in combinations)
|
92
|
-
{
|
93
|
-
for (var j = 0; j < combination.Length; j++)
|
94
|
-
{
|
95
|
-
var brush = combination[j].Value;
|
96
|
-
graphics.FillRectangle(brush, w / i * j, 0, w / i, h);
|
97
|
-
}
|
98
|
-
|
99
|
-
var keys = string.Join("-", combination.Select(x => x.Key));
|
100
|
-
var codes = string.Join("-", combination.Select(x => x.Value.Color)
|
101
|
-
.Select(x => $"#{x.R:X2}{x.G:X2}{x.B:X2}"));
|
102
|
-
|
103
|
-
var fileName = $"{count}_{keys}_{codes}.png";
|
104
|
-
canvas.Save(fileName, ImageFormat.Png);
|
105
|
-
count++;
|
106
|
-
}
|
107
|
-
}
|
108
|
-
}
|
109
|
-
}
|
110
|
-
}
|
111
|
-
|
112
|
-
|
113
|
-
// [C#:n個の要素からk個を選ぶ組合せを列挙する - Qiita](https://qiita.com/gushwell/items/74a96f56ccb64db3660c)
|
114
|
-
static class Combination
|
115
|
-
{
|
116
|
-
public static IEnumerable<T[]> Enumerate<T>(IEnumerable<T> items, int k, bool withRepetition)
|
117
|
-
{
|
118
|
-
if (k == 1)
|
119
|
-
{
|
120
|
-
foreach (var item in items) yield return new T[] { item };
|
121
|
-
yield break;
|
122
|
-
}
|
123
|
-
foreach (var item in items)
|
124
|
-
{
|
125
|
-
var leftside = new T[] { item };
|
126
|
-
var unused = withRepetition ? items : items.SkipWhile(e => !e.Equals(item)).Skip(1).ToList();
|
127
|
-
foreach (var rightside in Enumerate(unused, k - 1, withRepetition))
|
128
|
-
yield return leftside.Concat(rightside).ToArray();
|
129
|
-
}
|
130
|
-
}
|
131
|
-
}
|
132
|
-
}
|
133
|
-
```
|
134
|
-
|
1
|
+
参考コードがある場合は出典を**質問に明示**してください。
|
2
|
+
[C#:n個の要素からk個を選ぶ組合せを列挙する - Qiita](https://qiita.com/gushwell/items/74a96f56ccb64db3660c)
|
3
|
+
|
4
|
+
> 7色の組み合わせの画像を127通り作りたい
|
5
|
+
|
6
|
+
7個中から1個選ぶ重複しない組合せ:7
|
7
|
+
7個中から2個選ぶ重複しない組合せ:21
|
8
|
+
7個中から3個選ぶ重複しない組合せ:35
|
9
|
+
7個中から4個選ぶ重複しない組合せ:35
|
10
|
+
7個中から5個選ぶ重複しない組合せ:21
|
11
|
+
7個中から6個選ぶ重複しない組合せ:7
|
12
|
+
7個中から7個選ぶ重複しない組合せ:1
|
13
|
+
計:127通りということですね。そしてそれはもうできている。と
|
14
|
+
|
15
|
+
> ファイルが実現したいようにできない、127通りの画像を出力できない。
|
16
|
+
|
17
|
+
`Output_Click`がどういう意図か1ミリもわからないのですが、`canvas.Save`でpngファイルを作るのだから都度ファイル名を変えないといけないのでは?
|
18
|
+
|
19
|
+
> ファイル名を組み合わせた色にあわせるようにdictionaryを使って作れないか摸索
|
20
|
+
|
21
|
+
皆さんおっしゃっていますが、setunさんがどういうファイル名にしたいのかがまったく伝わっていません。
|
22
|
+
|
23
|
+
`ColorDialog`は1個で十分です。使いまわせるのでボタン分作る必要はありません。
|
24
|
+
|
25
|
+
全体的に変数名がひどすぎます。`Button`なのか`Label`なのか`TextBox`なのかまったくわかりません。
|
26
|
+
ひとりで作っているならどうでもいいですが、第三者に見せるのなら、わかりやすい名前に変える・説明をつける・Designer.csを提示する等の配慮が必要です。
|
27
|
+
|
28
|
+
|
29
|
+
やりたいこと自体は分かったので、3色版を書いてみました。
|
30
|
+
```cs
|
31
|
+
using System;
|
32
|
+
using System.Collections.Generic;
|
33
|
+
using System.Data;
|
34
|
+
using System.Drawing;
|
35
|
+
using System.Drawing.Imaging;
|
36
|
+
using System.Linq;
|
37
|
+
using System.Windows.Forms;
|
38
|
+
|
39
|
+
namespace Questions356810
|
40
|
+
{
|
41
|
+
public partial class Form1 : Form
|
42
|
+
{
|
43
|
+
public Form1()
|
44
|
+
{
|
45
|
+
InitializeComponent();
|
46
|
+
//color1Button.BackColor = Color.Red;
|
47
|
+
//color2Button.BackColor = Color.Lime;
|
48
|
+
//color3Button.BackColor = Color.Blue;
|
49
|
+
}
|
50
|
+
|
51
|
+
private void Color1Button_Click(object sender, EventArgs e)
|
52
|
+
{
|
53
|
+
if (colorDialog1.ShowDialog() == DialogResult.OK)
|
54
|
+
color1Button.BackColor = colorDialog1.Color;
|
55
|
+
}
|
56
|
+
private void Color2Button_Click(object sender, EventArgs e)
|
57
|
+
{
|
58
|
+
if (colorDialog1.ShowDialog() == DialogResult.OK)
|
59
|
+
color2Button.BackColor = colorDialog1.Color;
|
60
|
+
}
|
61
|
+
private void Color3Button_Click(object sender, EventArgs e)
|
62
|
+
{
|
63
|
+
if (colorDialog1.ShowDialog() == DialogResult.OK)
|
64
|
+
color3Button.BackColor = colorDialog1.Color;
|
65
|
+
}
|
66
|
+
|
67
|
+
private void OutputButton_Click(object sender, EventArgs e)
|
68
|
+
{
|
69
|
+
float w = 100;
|
70
|
+
float h = 100;
|
71
|
+
|
72
|
+
using (var canvas = new Bitmap((int)w, (int)h))
|
73
|
+
using (var graphics = Graphics.FromImage(canvas))
|
74
|
+
using (var brush1 = new SolidBrush(color1Button.BackColor))
|
75
|
+
using (var brush2 = new SolidBrush(color2Button.BackColor))
|
76
|
+
using (var brush3 = new SolidBrush(color3Button.BackColor))
|
77
|
+
{
|
78
|
+
// Combination.EnumerateはDictionaryでも使える
|
79
|
+
// 文字列との対応を取りたいのであればこれを渡せばいい
|
80
|
+
var choices = new Dictionary<string, SolidBrush>
|
81
|
+
{
|
82
|
+
{ "Color1", brush1 },
|
83
|
+
{ "Color2", brush2 },
|
84
|
+
{ "Color3", brush3 },
|
85
|
+
};
|
86
|
+
|
87
|
+
var count = 1;
|
88
|
+
for (var i = 1; i <= choices.Count; i++)
|
89
|
+
{
|
90
|
+
var combinations = Combination.Enumerate(choices, i, false);
|
91
|
+
foreach (var combination in combinations)
|
92
|
+
{
|
93
|
+
for (var j = 0; j < combination.Length; j++)
|
94
|
+
{
|
95
|
+
var brush = combination[j].Value;
|
96
|
+
graphics.FillRectangle(brush, w / i * j, 0, w / i, h);
|
97
|
+
}
|
98
|
+
|
99
|
+
var keys = string.Join("-", combination.Select(x => x.Key));
|
100
|
+
var codes = string.Join("-", combination.Select(x => x.Value.Color)
|
101
|
+
.Select(x => $"#{x.R:X2}{x.G:X2}{x.B:X2}"));
|
102
|
+
|
103
|
+
var fileName = $"{count}_{keys}_{codes}.png";
|
104
|
+
canvas.Save(fileName, ImageFormat.Png);
|
105
|
+
count++;
|
106
|
+
}
|
107
|
+
}
|
108
|
+
}
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
|
113
|
+
// [C#:n個の要素からk個を選ぶ組合せを列挙する - Qiita](https://qiita.com/gushwell/items/74a96f56ccb64db3660c)
|
114
|
+
static class Combination
|
115
|
+
{
|
116
|
+
public static IEnumerable<T[]> Enumerate<T>(IEnumerable<T> items, int k, bool withRepetition)
|
117
|
+
{
|
118
|
+
if (k == 1)
|
119
|
+
{
|
120
|
+
foreach (var item in items) yield return new T[] { item };
|
121
|
+
yield break;
|
122
|
+
}
|
123
|
+
foreach (var item in items)
|
124
|
+
{
|
125
|
+
var leftside = new T[] { item };
|
126
|
+
var unused = withRepetition ? items : items.SkipWhile(e => !e.Equals(item)).Skip(1).ToList();
|
127
|
+
foreach (var rightside in Enumerate(unused, k - 1, withRepetition))
|
128
|
+
yield return leftside.Concat(rightside).ToArray();
|
129
|
+
}
|
130
|
+
}
|
131
|
+
}
|
132
|
+
}
|
133
|
+
```
|
134
|
+
|
135
135
|

|