170 lines
5.1 KiB
C#
170 lines
5.1 KiB
C#
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 Paint_2
|
|
{
|
|
public partial class ColorPicker : Form
|
|
{
|
|
public Bitmap colorMap;
|
|
public int RedLevel = 0;
|
|
public int GreenLevel = 0;
|
|
public int BlueLevel = 0;
|
|
public Color SelectedColor;
|
|
public Point cursorLocation;
|
|
public PaintForm main;
|
|
public ColorPicker(PaintForm theMainForm)
|
|
{
|
|
InitializeComponent();
|
|
main = theMainForm;
|
|
}
|
|
|
|
private void ColorPicker_Load(object sender, EventArgs e)
|
|
{
|
|
colorMap = new Bitmap(255, 255);
|
|
SelectedColor = this.BackColor;
|
|
RefreshUi();
|
|
}
|
|
private Bitmap RefreshMap(Bitmap map)
|
|
{
|
|
Graphics gr = Graphics.FromImage(map);
|
|
for (int g = 0; g < 255; g += 1)
|
|
{
|
|
for (int b = 0; b < 255; b += 1)
|
|
{
|
|
map.SetPixel(g, b, Color.FromArgb(255, RedLevel, g, b));
|
|
}
|
|
}
|
|
return map;
|
|
}
|
|
private void RefreshUi()
|
|
{
|
|
pbxColor.Image = RefreshMap(colorMap);
|
|
Color Scol = Color.FromArgb(SelectedColor.A, SelectedColor.R, SelectedColor.G, SelectedColor.B);
|
|
tbrRedLevel.Value = RedLevel;
|
|
pbxSelectedColor.BackColor = Scol;
|
|
nupRed.Value = Scol.R;
|
|
nupGreen.Value = Scol.G;
|
|
nupBlue.Value = Scol.B;
|
|
nupGamma.Value = Scol.A;
|
|
tbxColorHex.Text = ColorToHex(Scol);
|
|
}
|
|
|
|
private void tbrRedLevel_Scroll(object sender, EventArgs e)
|
|
{
|
|
RedLevel = tbrRedLevel.Value;
|
|
RefreshUi();
|
|
}
|
|
|
|
private void pbxColor_Click(object sender, EventArgs e)
|
|
{
|
|
//Bitmap img = (Bitmap)pbxColor.Image;
|
|
cursorLocation = pbxColor.PointToClient(MousePosition);
|
|
SelectedColor = colorMap.GetPixel(cursorLocation.X, cursorLocation.Y);
|
|
RefreshUi();
|
|
//MessageBox.Show(cursorLocation.ToString()+Environment.NewLine+SelectedColor.ToString());
|
|
}
|
|
private Color HexToColor(string rgb)
|
|
{
|
|
int toBase = 16;
|
|
string raw = tbxColorHex.Text;
|
|
string cleaned = raw.Replace("#", String.Empty);
|
|
Color result;
|
|
if (cleaned.Length == 6)
|
|
{
|
|
try
|
|
{
|
|
int r = Convert.ToInt32(String.Concat(cleaned[0], cleaned[1]), toBase);
|
|
int g = Convert.ToInt32(String.Concat(cleaned[2], cleaned[3]), toBase);
|
|
int b = Convert.ToInt32(String.Concat(cleaned[4], cleaned[5]), toBase);
|
|
result = Color.FromArgb(r, g, b);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
result = default;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result = default;
|
|
}
|
|
return result;
|
|
}
|
|
private string ColorToHex(Color color)
|
|
{
|
|
int toBase = 16;
|
|
string result = "#";
|
|
|
|
if (color.R < toBase)
|
|
{
|
|
result += "0";
|
|
}
|
|
result += Convert.ToString(color.R, toBase);
|
|
if (color.G < toBase)
|
|
{
|
|
result += "0";
|
|
}
|
|
result += Convert.ToString(color.G, toBase);
|
|
if (color.B < toBase)
|
|
{
|
|
result += "0";
|
|
}
|
|
result += Convert.ToString(color.B, toBase);
|
|
|
|
return result;
|
|
}
|
|
|
|
private void nupValueChanged(object sender, EventArgs e)
|
|
{
|
|
int tmpR = (int)nupRed.Value;
|
|
int tmpG = (int)nupGreen.Value;
|
|
int tmpB = (int)nupBlue.Value;
|
|
int tmpA = (int)nupGamma.Value;
|
|
|
|
if (tmpR >= 0 && tmpR <= 255 && tmpG >= 0 && tmpG <= 255 && tmpB >= 0 && tmpB <= 255 && tmpA >= 0 && tmpA <= 255)
|
|
{
|
|
Color newColor = Color.FromArgb(tmpA, tmpR, tmpG, tmpB);
|
|
if (SelectedColor != newColor)
|
|
{
|
|
SelectedColor = newColor;
|
|
RedLevel = tmpR;
|
|
RefreshUi();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void tbxColorHex_TextChanged(object sender, EventArgs e)
|
|
{
|
|
Color tmp = HexToColor(tbxColorHex.Text);
|
|
if (tmp != null)
|
|
{
|
|
if (SelectedColor != tmp)
|
|
{
|
|
SelectedColor = tmp;
|
|
RedLevel = tmp.R;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ColorPicker_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
main.sketch.ChangePaintToolColor(SelectedColor);
|
|
}
|
|
|
|
private void tbxColorHex_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.KeyCode == Keys.Enter)
|
|
{
|
|
RefreshUi();
|
|
}
|
|
}
|
|
}
|
|
}
|