-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathColorForm.cs
228 lines (189 loc) · 7.78 KB
/
ColorForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PasiveRadar
{
public partial class ColorForm : Form
{
public delegate void MyDelegate(Color[] color_table);
public static event MyDelegate EventColor;
public bool AcceptColors;
private struct ColorPoint
{
public int PointPosition;
public Color color;
}
public Color[] ColorTable = new Color[Draw.ColorTableSize];
private List<ColorPoint> ListOfPoints = new List<ColorPoint>();
public ColorForm(Color[] LocalColorTable)
{
InitializeComponent();
//Set the color array
Array.Copy(LocalColorTable, ColorTable, Draw.ColorTableSize);
//Add the first point to be black
ColorPoint ColPoint = new PasiveRadar.ColorForm.ColorPoint();
ColPoint.PointPosition = 0;
ColPoint.color = Color.FromArgb(255, 0, 0, 0);
ListOfPoints.Add(ColPoint);
DrawTable();
}
private void button_Ok(object sender, EventArgs e)
{
//Get the last color
if (ListOfPoints.Count > 1)
{
Color color = ListOfPoints.Last().color;
//Set the last point in color table to the last color in the List
ColorPoint ColPoint = new PasiveRadar.ColorForm.ColorPoint();
ColPoint.PointPosition = Draw.ColorTableSize;
ColPoint.color = color;
FillTableWithColorValues(ColPoint);
}
DrawTable();
if (EventColor != null)
EventColor(ColorTable);
AcceptColors = true;
this.Close();
}
//Cancel
private void button_Cancel(object sender, EventArgs e)
{
AcceptColors = false;
this.Close();
}
//Converts position from the picture to position in the table
private int ConvertPositionToTable(int PositionX)
{
int Factor;
if (pictureBox1.Width > 0)
Factor = Draw.ColorTableSize / pictureBox1.Width;
else return 0;
return Factor * PositionX;
}
//Converts position in the table to position on the picture
private int ConvertTableToPosition(int TablePositionX)
{
int Factor;
int PicturePosition;
if (pictureBox1.Width > 0)
Factor = Draw.ColorTableSize / pictureBox1.Width;
else return 0;
PicturePosition = TablePositionX / Factor;
if (PicturePosition >= pictureBox1.Width) PicturePosition = pictureBox1.Width - 1;
return PicturePosition;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// Convert position to th table position
int Table_position = ConvertPositionToTable(e.X);
//Add the color point to the table
ColorPoint ColPoint = new PasiveRadar.ColorForm.ColorPoint();
ColPoint.PointPosition = Table_position;
colorDialog1.ShowDialog();
ColPoint.color = colorDialog1.Color;// Color.FromArgb(255, 255, 0);
FillTableWithColorValues(ColPoint);
DrawTable();
}
private void DrawTable()
{
//Draw color table
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
int x, y;
for (y = 0; y < pictureBox1.Height; y++)
{
for (int i = 0; i < Draw.ColorTableSize; i++)
{
x = ConvertTableToPosition(i);
((Bitmap)pictureBox1.Image).SetPixel(x, y, ColorTable[i]);
}
}
//second picture
pictureBox2.Image = new Bitmap(pictureBox2.Width, pictureBox2.Height);
for (y = 0; y < pictureBox2.Height; y++)
{
for (int i = 0; i < ListOfPoints.Count; i++)
{
x = ConvertTableToPosition(ListOfPoints.ElementAt(i).PointPosition);
// Nice tringle
for (int k = 0; k < 10; k++)
if (x - k > 0 && x + k < pictureBox2.Image.Width)
if ( y + k < pictureBox2.Image.Height)
{
((Bitmap)pictureBox2.Image).SetPixel(x - k, y + k, ListOfPoints.ElementAt(i).color);
((Bitmap)pictureBox2.Image).SetPixel(x + k, y + k, ListOfPoints.ElementAt(i).color);
}
}
}
}
private void FillTableWithColorValues(ColorPoint ColPoint)
{
int LastPositionX = 0;
ListOfPoints.Add(ColPoint);
//Sort the list
ListOfPoints = ListOfPoints.OrderBy(sel => sel.PointPosition).ToList();
for (int i = 1; i < ListOfPoints.Count; i++)
{
//Fill the colors in the table
for (int j = LastPositionX; j < ListOfPoints.ElementAt(i).PointPosition; j++)
{
ColorTable[j] = LerpColors(j, LastPositionX, ListOfPoints.ElementAt(i).PointPosition, ListOfPoints.ElementAt(i - 1).color, ListOfPoints.ElementAt(i).color);
}
LastPositionX = ListOfPoints.ElementAt(i).PointPosition;
}
}
Color LerpColors(int position, int previousPosition, int EndPosition, Color StartColor, Color EndColor)
{
int CorrectedCurrentPosition = position - previousPosition;
int CorrectedLastPosition = EndPosition - previousPosition;
float delR = EndColor.R - StartColor.R;
float delG = EndColor.G - StartColor.G;
float delB = EndColor.B - StartColor.B;
float fraction = 1.0f * CorrectedCurrentPosition / CorrectedLastPosition;
if (fraction == 0) fraction = 0.01f;
int r = (int)(StartColor.R + delR * fraction);
if (r < 0) r = 0; if (r > 255) r = 255;
int g = (int)(StartColor.G + delG * fraction);
if (g < 0) g = 0; if (g > 255) g = 255;
int b = (int)(StartColor.B + delB * fraction);
if (b < 0) b = 0; if (b > 255) b = 255;
return Color.FromArgb(255, r, g, b);
}
private void button2_Click(object sender, EventArgs e)
{
ListOfPoints.Clear();
Array.Clear(ColorTable, 0, Draw.ColorTableSize);
//The first point is black
ColorPoint ColPoint = new PasiveRadar.ColorForm.ColorPoint();
ColPoint.PointPosition = 0;
ColPoint.color = Color.FromArgb(255, 0, 0, 0);
ListOfPoints.Add(ColPoint);
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
int x, y;
for (y = 0; y < pictureBox1.Height; y++)
{
for (x = 0; x < pictureBox1.Width; x++)
{
((Bitmap)pictureBox1.Image).SetPixel(x, y, Color.FromArgb(255, 0, 0, 0));
}
}
pictureBox2.Image = new Bitmap(pictureBox2.Width, pictureBox2.Height);
for (y = 0; y < pictureBox2.Height; y++)
{
for (x = 0; x < pictureBox2.Width; x++)
{
((Bitmap)pictureBox2.Image).SetPixel(x, y, Color.FromArgb(255, 0, 0, 0));
}
}
}
private void ColorForm_ResizeEnd(object sender, EventArgs e)
{
DrawTable();
}
}
}