效果图
xaml代码
c#代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Documents.DocumentStructures;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApp_HuaYiHotel.Model;
namespace WpfApp_HuaYiHotel.MoKuai.FangTai
{
///
/// MyColor.xaml 的交互逻辑
///
public partial class MyColor : Window
{
private readonly Model.HuaYiHotel_SQLEntities myModel = new Model.HuaYiHotel_SQLEntities();
readonly System.Drawing.Bitmap baseBack;//底板位图对象
System.Drawing.Bitmap lightBmp;//亮度位图对象
System.Drawing.Bitmap ColorBmp;//色谱位图对象
System.Drawing.Color lightColor = System.Drawing.Color.Black;//亮度色谱
int ColorPx = -10, ColorPy = -10;//选择颜色的坐标
int lightP = 121;//选择亮度的坐标
readonly int ColorSelectorWidth = 18;//rectColorSelector的宽度值
readonly int ColorSelectorHeight = 129;//rectColorSelector的top值
public Color returnSelectColor;//选择的颜色
static public string DangQianYanSe;
readonly byte fangTaiID;
public MyColor(byte myfangTaiID)
{
InitializeComponent();
//ZiDingYiYanSe();
ColorUserDefined();
FuZhiWrapPanelYanSeMoBan();
fangTaiID = myfangTaiID;
SetColorBack();
SetlightBack();
baseBack = SetBaseColorBack();
ColorPx = 0;
ColorPy = 0;
returnSelectColor = Colors.Black;
//text_color.Text = Convert.ToString(returnSelectColor);
//text_color.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#003D3D3D"));
textBlock_DangQianYanSeZhi.Text = Convert.ToString(returnSelectColor);
SelectColor.Background = new SolidColorBrush(returnSelectColor);
//DangQianYanSe= Convert.ToString(returnSelectColor);
#region 事件绑定
image_GuanBi.MouseLeftButtonDown += Image_GuanBi_MouseLeftButtonDown;
button_QuXiao.Click += Button_QuXiao_Click;
button_QueRen.Click += Button_QueRen_Click;
//borderMyColor1.MouseRightButtonUp += BorderMyColor_MouseRightButtonUp;
//borderMyColor2.MouseRightButtonUp += BorderMyColor2_MouseRightButtonUp;
#endregion
}
private void ColorUserDefined()
{
string[] stringColorUserDefined =
{
Properties.Settings.Default.myColor1,
Properties.Settings.Default.myColor2,
Properties.Settings.Default.myColor3,
Properties.Settings.Default.myColor4,
Properties.Settings.Default.myColor5,
Properties.Settings.Default.myColor6,
Properties.Settings.Default.myColor7,
Properties.Settings.Default.myColor8,
Properties.Settings.Default.myColor9,
Properties.Settings.Default.myColor10,
Properties.Settings.Default.myColor11,
Properties.Settings.Default.myColor12,
Properties.Settings.Default.myColor13,
Properties.Settings.Default.myColor14,
Properties.Settings.Default.myColor15,
};
Border[] myBorder = new Border[stringColorUserDefined.Length];
for (int i = 0; i < stringColorUserDefined.Length; i++)
{
myBorder[i] = new Border()
{
Name = "borderColorUserDefined" + i.ToString(),
Height = 35,
Width = 35,
Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(stringColorUserDefined[i])),
Margin = new Thickness(7, 5, 7, 5),
BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("red"))
};
myBorder[i].MouseLeftButtonDown += ColorMoBan;
myBorder[i].MouseRightButtonDown += MyColor_MouseRightButtonDown;
wrapPanelYanSeMoBan.Children.Add(myBorder[i]);
}
}
private void MyColor_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
//Control col = this.myBorder.Controls.Find("控件名", true)[0];
//Button btn = col as Button;//转为Button
}
private void FuZhiWrapPanelYanSeMoBan()
{
string[] stringClolrMoBan = { "#FFFF1300", "#FFFF6F00", "#FFFFA200", "#FFFFD500", "#FFF2FB00", "#FF89E100", "#FF00CC2A", "#FF008DCC", "#FF003FCC", "#FF3400CC", "#FFA600CC", "#FFDD0061", "#FF99FFFF", "#FF6F66FF", "#FFF2FFBF" };
Border[] myBorder1 = new Border[stringClolrMoBan.Length];
for (int i = 0; i < stringClolrMoBan.Length; i++)
{
myBorder1[i] = new Border()
{
Name = "border_YanSeMoBan" + i.ToString(),
Height = 35,
Width = 35,
Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(stringClolrMoBan[i])),
//Tag = Convert.ToString(FangTaiBiao.Rows[i]["FangJianZhuangTaiID"])
Margin = new Thickness(7, 5, 7, 5),
BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("red"))
};
myBorder1[i].MouseLeftButtonDown += ColorMoBan; //Border_XianZhuZhuangTaiColor_MouseLeftButtonDown;
wrappanel_color.Children.Add(myBorder1[i]);
}
}
private void ColorMoBan(object sender, MouseButtonEventArgs e)
{
SelectColor.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(sender.GetType().GetProperty("Background").GetValue(sender).ToString()));
textBlock_DangQianYanSeZhi.Text = sender.GetType().GetProperty("Background").GetValue(sender).ToString();
}
//private void BorderMyColor2_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
//{
// borderMyColor2.Background = new SolidColorBrush(returnSelectColor);
// Properties.Settings.Default.myColor2 = Convert.ToString(returnSelectColor);
//}
//private void BorderMyColor_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
//{
// borderMyColor1.Background = new SolidColorBrush(returnSelectColor);
// Properties.Settings.Default.myColor1 = Convert.ToString(returnSelectColor);
//}
//public void ZiDingYiYanSe()
//{
// borderMyColor1.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Properties.Settings.Default.myColor1));
// borderMyColor2.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Properties.Settings.Default.myColor2));
//}
public void FangTaiYanSeGengGai(byte fangTaiID, string Color)
{
var fangTaiXinXi = (from tbFangJianZhuangTai in myModel.F_FangJianZhuangTai
where fangTaiID == tbFangJianZhuangTai.FangJianZhuangTaiID
select new
{
dbFangTaiMingCheng = tbFangJianZhuangTai.ZhuangTaiMingcCheng,
dbFangJianDaiMa = tbFangJianZhuangTai.ZhuangTaiDaiMa
}).SingleOrDefault();
try
{
//实例化对象
F_FangJianZhuangTai myFangJianZhuangTai = new F_FangJianZhuangTai
{
FangJianZhuangTaiID = fangTaiID,
YanSe = Color,
ZhuangTaiDaiMa = fangTaiXinXi.dbFangJianDaiMa,
ZhuangTaiMingcCheng = fangTaiXinXi.dbFangTaiMingCheng
};
myModel.Entry(myFangJianZhuangTai).State = System.Data.Entity.EntityState.Modified;
//保存修改
myModel.SaveChanges();
}
catch (Exception)
{
throw;
}
}
private void Button_QueRen_Click(object sender, RoutedEventArgs e)
{
//DangQianYanSe = Convert.ToString(returnSelectColor);
//string strMsg = "failed";
//try
//{
// //实例化对象
// SYS_Academe myAcademe = new SYS_Academe();
// //修改数据
// myAcademe.AcademeID = ID;
// myAcademe.AcademeName = Name;
// myAcademe.AcademeCode = Code;
// //获取和实质对象实体的状态=EntityState的枚举值
// myModels.Entry(myAcademe).State = EntityState.Modified;
// //保存修改
// myModels.SaveChanges();
// strMsg = "success";
//}
//catch (Exception)
//{
// strMsg = "failed";
//}
FangTaiYanSeGengGai(fangTaiID, DangQianYanSe);
this.Close();
}
private void Button_QuXiao_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void Image_GuanBi_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.Close();
}
///
/// 获得色谱位图对象
///
public void SetColorBack()
{
ColorBmp = new System.Drawing.Bitmap(205, ColorSelectorHeight);
_ = Colors.White;
int R = 255, G = 0, B = 0;
for (int i = 0; i < ColorSelectorHeight; i++)
{
for (int j = 0; j < 205; j++)
{
if (j >= 0 && j <= 34)
G = SetColorNum(G, j, true);
if (j > 34 && j <= 68)
R = SetColorNum(R, j, false);
if (j > 68 && j <= 102)
B = SetColorNum(B, j, true);
if (j > 102 && j <= 136)
G = SetColorNum(G, j, false);
if (j > 136 && j <= 170)
R = SetColorNum(R, j, true);
if (j > 170 && j <= 204)
B = SetColorNum(B, j, false);
int temps = i * -2;
int R2 = IntMaxMin(R + temps);
int G2 = IntMaxMin(G + temps);
int B2 = IntMaxMin(B + temps);
int l = Math.Max(0, lightP == 0 ? 255 : (121 - lightP) * 2);//lightP原始值
System.Drawing.Color tmpC = SetColorWhite(System.Drawing.Color.FromArgb(R2, G2, B2), l);
Color tempColor = Color.FromRgb(tmpC.R, tmpC.G, tmpC.B);
ColorBmp.SetPixel(j, i, System.Drawing.Color.FromArgb(tempColor.R, tempColor.G, tempColor.B));
}
}
}
///
/// 设置色谱位图
///
public System.Drawing.Bitmap SetBaseColorBack()
{
ColorBmp = new System.Drawing.Bitmap(205, ColorSelectorHeight);
_ = System.Drawing.Color.White;
int R = 255, G = 0, B = 0;
for (int i = 0; i < ColorSelectorHeight; i++)
{
for (int j = 0; j < 205; j++)
{
if (j >= 0 && j <= 34)
G = SetColorNum(G, j, true);
if (j > 34 && j <= 68)
R = SetColorNum(R, j, false);
if (j > 68 && j <= 102)
B = SetColorNum(B, j, true);
if (j > 102 && j <= 136)
G = SetColorNum(G, j, false);
if (j > 136 && j <= 170)
R = SetColorNum(R, j, true);
if (j > 170 && j <= 204)
B = SetColorNum(B, j, false);
int temps = i * -2;
int R2 = IntMaxMin(R + temps);
int G2 = IntMaxMin(G + temps);
int B2 = IntMaxMin(B + temps);
System.Drawing.Color tempColor = System.Drawing.Color.FromArgb(R2, G2, B2);
ColorBmp.SetPixel(j, i, tempColor);
}
}
//将Bitmap转换为BitmapSource
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(ColorBmp);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
ColorBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
BitmapImage tempImage = new BitmapImage();
tempImage.BeginInit();
tempImage.StreamSource = new System.IO.MemoryStream(ms.ToArray());
tempImage.EndInit();
imgColor.Source = tempImage;
}
g.Dispose();
return ColorBmp;
}
///
/// 设置亮度位图
///
public void SetlightBack()
{
lightBmp = new System.Drawing.Bitmap(ColorSelectorWidth, ColorSelectorHeight);
for (int i = 0; i < ColorSelectorHeight; i++)
{
for (int j = 0; j < ColorSelectorWidth; j++)
{
System.Drawing.Color c = SetColorWhite(lightColor, Math.Max(255 - i * 2, 0));
lightBmp.SetPixel(j, i, c);
}
}
//将Bitmap转换为BitmapSource
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(lightBmp);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
lightBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
BitmapImage tempImage = new BitmapImage();
tempImage.BeginInit();
tempImage.StreamSource = new System.IO.MemoryStream(ms.ToArray());
tempImage.EndInit();
imgColorSelector.Source = tempImage;
}
g.Dispose();
}
///
/// 设置具体坐标的色彩值
///
public int SetColorNum(int self, int y, bool isAdd)
{
int addnum = 8;
if (y == 0)
return self;
int num = isAdd ? self + addnum : self - addnum;
return IntMaxMin(num);
}
///
/// 颜色变量区域限制
///
public int IntMaxMin(int num)
{
if (num > 255)
return 255;
if (num < 0)
return 0;
return num;
}
///
/// 设置颜色为白色
///
public System.Drawing.Color SetColorWhite(System.Drawing.Color c, int white = 50)
{
int R = Math.Min(c.R + white, 255);
int G = Math.Min(c.G + white, 255);
int B = Math.Min(c.B + white, 255);
return System.Drawing.Color.FromArgb(R, G, B);
}
#region 调色板鼠标事件
private void ImgColor_MouseDown(object sender, MouseButtonEventArgs e)
{
imgColor.MouseMove += new MouseEventHandler(ImgColor_MouseMove);
SetColorPoint(e);
}
private void ImgColor_MouseMove(object sender, MouseEventArgs e)
{
SetColorPoint(e);
}
private void ImgColor_MouseUp(object sender, MouseButtonEventArgs e)
{
imgColor.MouseMove -= new MouseEventHandler(ImgColor_MouseMove);
}
///
/// 同时设置颜色选择点位置
///
public void SetColorPoint(MouseEventArgs e)
{
ColorPx = (int)(e.GetPosition(imgColor).X - 4);
ColorPy = (int)(e.GetPosition(imgColor).Y - 4);
if (ColorPx > ColorBmp.Width - 1)
ColorPx = ColorBmp.Width - 1;
if (ColorPx < 0)
ColorPx = 0;
if (ColorPy > ColorBmp.Height - 1)
ColorPy = ColorBmp.Height - 1;
if (ColorPy < 0)
ColorPy = 0;
ImgColorShowChange();
if (ColorPx >= 0 && ColorPx <= ColorBmp.Width && ColorPy >= 0 && ColorPy <= ColorBmp.Height)
{
lightColor = baseBack.GetPixel(ColorPx, ColorPy);
System.Drawing.Color tmpClor = ColorRight(ColorBmp.GetPixel(ColorPx, ColorPy));
returnSelectColor = Color.FromRgb(tmpClor.R, tmpClor.G, tmpClor.B);
//text_color.Text = Convert.ToString(returnSelectColor);
textBlock_DangQianYanSeZhi.Text = Convert.ToString(returnSelectColor);
SelectColor.Background = new SolidColorBrush(returnSelectColor);
DangQianYanSe = Convert.ToString(returnSelectColor);
SetlightBack();
}
}
private void ImgColorShowChange()
{
Canvas.SetTop(this.ellColor, ColorPy - 4);
Canvas.SetLeft(this.ellColor, ColorPx - 4);
}
///
/// 颜色校正
///
public System.Drawing.Color ColorRight(System.Drawing.Color c)
{
int R = IntMaxMin(c.R);
int G = IntMaxMin(c.G);
int B = IntMaxMin(c.B);
return System.Drawing.Color.FromArgb(R, G, B);
}
#endregion
#region 亮度调节鼠标事件
private void ImgColorSelector_MouseDown(object sender, MouseButtonEventArgs e)
{
imgColorSelector.MouseMove += new MouseEventHandler(ImgColorSelector_MouseMove);
SetLightPoint(e);
}
private void ImgColorSelector_MouseMove(object sender, MouseEventArgs e)
{
SetLightPoint(e);
}
private void ImgColorSelector_MouseUp(object sender, MouseButtonEventArgs e)
{
imgColorSelector.MouseMove -= new MouseEventHandler(ImgColorSelector_MouseMove);
}
///
/// 同步设置亮度选择点位置
///
public void SetLightPoint(MouseEventArgs e)
{
lightP = (int)(e.GetPosition(imgColorSelector).Y - 2);
if (lightP > ColorSelectorHeight)
lightP = ColorSelectorHeight;
if (lightP < 0)
lightP = 0;
SetColorBack();
ImgColorShowChange();
ImgColorSelectorChange();
if (ColorPx >= 0 && ColorPx <= ColorBmp.Width && ColorPy >= 0 && ColorPy <= ColorBmp.Height)
{
lightColor = baseBack.GetPixel(ColorPx, ColorPy);
System.Drawing.Color tmpClor = ColorRight(ColorBmp.GetPixel(ColorPx, ColorPy));
returnSelectColor = Color.FromRgb(tmpClor.R, tmpClor.G, tmpClor.B);
//text_color.Text = Convert.ToString(returnSelectColor);
textBlock_DangQianYanSeZhi.Text = Convert.ToString(returnSelectColor);
SelectColor.Background = new SolidColorBrush(returnSelectColor);
DangQianYanSe = Convert.ToString(returnSelectColor);
SetlightBack();
}
}
private void ImgColorSelectorChange()
{
Canvas.SetTop(this.rectColorSelector, lightP);
}
#endregion
}
}



