From 0cfc0f9c50e24651d8954e05653e6e284fa9a2fb Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Wed, 19 Oct 2016 18:22:30 +0200 Subject: [PATCH] =?utf8?q?Basic=20color=20picker=20widget=20=09modifi?= =?utf8?q?=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20Crow.csproj=20=09modifi?= =?utf8?q?=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20CrowIDE/CrowIDE.csproj?= =?utf8?q?=20=09nouveau=20fichier=C2=A0:=20CrowIDE/Default.style=20=09modi?= =?utf8?q?fi=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20Default.style=20=09nou?= =?utf8?q?veau=20fichier=C2=A0:=20Templates/ColorPicker.template=20=09nouv?= =?utf8?q?eau=20fichier=C2=A0:=20Tests/Interfaces/Divers/colorPicker.crow?= =?utf8?q?=20=09nouveau=20fichier=C2=A0:=20src/GraphicObjects/ColorPicker.?= =?utf8?q?cs=20=09nouveau=20fichier=C2=A0:=20src/GraphicObjects/ColorSelec?= =?utf8?q?tor.cs=20=09modifi=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20Tests/?= =?utf8?q?Tests.csproj=20=09modifi=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20?= =?utf8?q?src/CompilerServices/CompilerServices.cs=20=09modifi=C3=A9=C2=A0?= =?utf8?q?:=20=20=20=20=20=20=20=20=20src/SolidColor.cs=20simplify=20Align?= =?utf8?q?ment=20=09modifi=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20src/Enum?= =?utf8?q?s.cs=20prevent=20null=20background=20fill=20=09modifi=C3=A9?= =?utf8?q?=C2=A0:=20=20=20=20=20=20=20=20=20src/GraphicObjects/GraphicObje?= =?utf8?q?ct.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Crow.csproj | 5 + CrowIDE/CrowIDE.csproj | 1 + CrowIDE/Default.style | 3 + Default.style | 6 + Templates/ColorPicker.template | 31 ++++ Tests/Interfaces/Divers/colorPicker.crow | 2 + Tests/Tests.csproj | 3 + src/CompilerServices/CompilerServices.cs | 2 +- src/Enums.cs | 2 +- src/GraphicObjects/ColorPicker.cs | 96 ++++++++++++ src/GraphicObjects/ColorSelector.cs | 184 +++++++++++++++++++++++ src/GraphicObjects/GraphicObject.cs | 2 + src/SolidColor.cs | 2 +- 13 files changed, 336 insertions(+), 3 deletions(-) create mode 100644 CrowIDE/Default.style create mode 100755 Templates/ColorPicker.template create mode 100755 Tests/Interfaces/Divers/colorPicker.crow create mode 100644 src/GraphicObjects/ColorPicker.cs create mode 100644 src/GraphicObjects/ColorSelector.cs diff --git a/Crow.csproj b/Crow.csproj index 5eba59f8..d34bcc36 100644 --- a/Crow.csproj +++ b/Crow.csproj @@ -146,6 +146,8 @@ + + @@ -277,6 +279,9 @@ Crow.Menu.template + + Crow.ColorPicker.template + diff --git a/CrowIDE/CrowIDE.csproj b/CrowIDE/CrowIDE.csproj index 0320b343..d4990c50 100644 --- a/CrowIDE/CrowIDE.csproj +++ b/CrowIDE/CrowIDE.csproj @@ -93,6 +93,7 @@ + diff --git a/CrowIDE/Default.style b/CrowIDE/Default.style new file mode 100644 index 00000000..19be18b7 --- /dev/null +++ b/CrowIDE/Default.style @@ -0,0 +1,3 @@ +TextBox { + Margin = 1; +} \ No newline at end of file diff --git a/Default.style b/Default.style index 933f89fe..42e09598 100644 --- a/Default.style +++ b/Default.style @@ -55,6 +55,7 @@ TextBox { Foreground = Black; Selectable = True; Text = TextBox; + Margin = 1; } Window { Focusable = true; @@ -80,4 +81,9 @@ Icon { Control { Margin=0; Spacing=3; +} +ColorSpinner { + Minimum = 0; + Maximum = 255; + SmallIncrement = 1; } \ No newline at end of file diff --git a/Templates/ColorPicker.template b/Templates/ColorPicker.template new file mode 100755 index 00000000..ececc7ac --- /dev/null +++ b/Templates/ColorPicker.template @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Tests/Interfaces/Divers/colorPicker.crow b/Tests/Interfaces/Divers/colorPicker.crow new file mode 100755 index 00000000..724ca4db --- /dev/null +++ b/Tests/Interfaces/Divers/colorPicker.crow @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 771c8b4a..e1b8e182 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -301,6 +301,9 @@ PreserveNewest + + PreserveNewest + diff --git a/src/CompilerServices/CompilerServices.cs b/src/CompilerServices/CompilerServices.cs index 305c9a5f..77666af2 100644 --- a/src/CompilerServices/CompilerServices.cs +++ b/src/CompilerServices/CompilerServices.cs @@ -308,7 +308,7 @@ namespace Crow if (b.Target.Member != null) il.Emit (OpCodes.Ldstr, b.Target.Member.Name); else - il.Emit (OpCodes.Ldstr, b.Expression.Split ('/').LastOrDefault ()); + il.Emit (OpCodes.Ldstr, b.Expression.Split ('/').LastOrDefault ().Split('.').LastOrDefault()); il.Emit (OpCodes.Ldc_I4_4);//StringComparison.Ordinal il.Emit (OpCodes.Callvirt, stringEquals); il.Emit (OpCodes.Brtrue, jumpTable [i]); diff --git a/src/Enums.cs b/src/Enums.cs index aac12e16..46f8cdab 100644 --- a/src/Enums.cs +++ b/src/Enums.cs @@ -11,7 +11,7 @@ namespace Crow Vertical } - public enum Alignment : byte + public enum Alignment { Top = 0x01, Left = 0x02, diff --git a/src/GraphicObjects/ColorPicker.cs b/src/GraphicObjects/ColorPicker.cs new file mode 100644 index 00000000..12729144 --- /dev/null +++ b/src/GraphicObjects/ColorPicker.cs @@ -0,0 +1,96 @@ +// +// ColorPicker.cs +// +// Author: +// Jean-Philippe Bruyère +// +// Copyright (c) 2016 jp +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using System.Xml.Serialization; +using System.ComponentModel; + +namespace Crow +{ + public class ColorPicker : TemplatedControl + { + public ColorPicker () : base () + { + } + + Color selectedColor; + float _red, _green, _blue, _alpha; + + [XmlAttributeAttribute()] + public virtual double Red { + get { return selectedColor.R; } + set { + if (selectedColor.R == value) + return; + selectedColor.R = value; + NotifyValueChanged ("Red", selectedColor.R); + NotifyValueChanged ("SelectedColor", new SolidColor(selectedColor)); + } + } + [XmlAttributeAttribute()] + public virtual double Green { + get { return selectedColor.G; } + set { + if (selectedColor.G == value) + return; + selectedColor.G = value; + NotifyValueChanged ("Green", selectedColor.G); + NotifyValueChanged ("SelectedColor", new SolidColor(selectedColor)); + } + } + [XmlAttributeAttribute()] + public virtual double Blue { + get { return selectedColor.B; } + set { + if (selectedColor.B == value) + return; + selectedColor.B = value; + NotifyValueChanged ("Blue", selectedColor.B); + NotifyValueChanged ("SelectedColor", new SolidColor(selectedColor)); + } + } + [XmlAttributeAttribute()] + public virtual double Alpha { + get { return selectedColor.A; } + set { + if (selectedColor.A == value) + return; + selectedColor.A = value; + NotifyValueChanged ("Alpha", selectedColor.A); + NotifyValueChanged ("SelectedColor", new SolidColor(selectedColor)); + } + } + [XmlAttributeAttribute()][DefaultValue("White")] + public virtual SolidColor SelectedColor { + get { return selectedColor; } + set { + if (selectedColor.Equals(value)) + return; + selectedColor = value; + NotifyValueChanged ("SelectedColor", new SolidColor(selectedColor)); + NotifyValueChanged ("Alpha", selectedColor.A); + NotifyValueChanged ("Blue", selectedColor.B); + NotifyValueChanged ("Green", selectedColor.G); + NotifyValueChanged ("Red", selectedColor.R); + } + } + } +} + diff --git a/src/GraphicObjects/ColorSelector.cs b/src/GraphicObjects/ColorSelector.cs new file mode 100644 index 00000000..a5b9d042 --- /dev/null +++ b/src/GraphicObjects/ColorSelector.cs @@ -0,0 +1,184 @@ +// +// ColorPicker.cs +// +// Author: +// Jean-Philippe Bruyère +// +// Copyright (c) 2016 jp +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using System.Xml.Serialization; +using System.ComponentModel; + +namespace Crow +{ + public class ColorSelector : GraphicObject + { + const double colDiv = 1.0 / 255.0; + + public ColorSelector ():base() + { + } + protected override void onDraw (Cairo.Context gr) + { + base.onDraw (gr); + + Rectangle r = ClientRectangle; + +// if (r.Width > r.Height) { +// int diff = r.Width - r.Height; +// r.Width = r.Height; +// r.X -= diff / 2; +// } else if (r.Height > r.Width) { +// int diff = r.Height - r.Width; +// r.Height = r.Width; +// r.Y += diff / 2; +// } + + Crow.Gradient grad = new Gradient (Gradient.Type.Horizontal); + + grad.Stops.Add (new Gradient.ColorStop (0, new Color (1, 0, 0, 1))); + grad.Stops.Add (new Gradient.ColorStop (0.2, new Color (1, 1, 0, 1))); + grad.Stops.Add (new Gradient.ColorStop (0.4, new Color (0, 1, 0, 1))); + grad.Stops.Add (new Gradient.ColorStop (0.6, new Color (0, 1, 1, 1))); + grad.Stops.Add (new Gradient.ColorStop (0.8, new Color (0, 0, 1, 1))); + grad.Stops.Add (new Gradient.ColorStop (1, new Color (1, 0, 0, 1))); + + grad.SetAsSource (gr, r); + CairoHelpers.CairoRectangle (gr, r, CornerRadius); + gr.Fill(); + + grad = new Gradient (Gradient.Type.Vertical); + grad.Stops.Add (new Gradient.ColorStop (0, new Color (1, 1, 1, 1))); + grad.Stops.Add (new Gradient.ColorStop (0.5, new Color (0, 0, 0, 0))); + grad.Stops.Add (new Gradient.ColorStop (1, new Color (0, 0, 0, 1))); + + grad.SetAsSource (gr, r); + CairoHelpers.CairoRectangle (gr, r, CornerRadius); + gr.Fill(); + } + [XmlAttributeAttribute()] + public virtual double R { + get { return Math.Ceiling((selectedColor as SolidColor).color.R * 255.0); } + set { + if (R == value) + return; + Color c = (SelectedColor as SolidColor).color; + SelectedColor = new SolidColor (new Color (value * colDiv, c.G, c.B, c.A)); + } + } + [XmlAttributeAttribute()] + public virtual double G { + get { return Math.Ceiling((selectedColor as SolidColor).color.G * 255.0); } + set { + if (G == value) + return; + Color c = (SelectedColor as SolidColor).color; + SelectedColor = new SolidColor (new Color (c.R, value * colDiv, c.B, c.A)); + } + } + [XmlAttributeAttribute()] + public virtual double B { + get { return Math.Ceiling((selectedColor as SolidColor).color.B * 255.0); } + set { + if (B == value) + return; + Color c = (SelectedColor as SolidColor).color; + SelectedColor = new SolidColor (new Color (c.R, c.G, value * colDiv, c.A)); + } + } + [XmlAttributeAttribute()] + public virtual double A { + get { return Math.Ceiling((selectedColor as SolidColor).color.A * 255.0); } + set { + if (A == value) + return; + Color c = (SelectedColor as SolidColor).color; + SelectedColor = new SolidColor (new Color (c.R, c.G, c.B, value * colDiv)); + } + } + + Fill pointedColor, selectedColor; + [XmlAttributeAttribute()][DefaultValue("White")] + public virtual Fill PointedColor { + get { return pointedColor; } + set { + if (pointedColor == value) + return; + pointedColor = value; + NotifyValueChanged ("PointedColor", pointedColor); + string n = (pointedColor as SolidColor).color.ToString (); + if (char.IsLetter(n[0])) + NotifyValueChanged ("PointedColorName", n); + else + NotifyValueChanged ("PointedColorName", "-"); + } + } + [XmlAttributeAttribute()][DefaultValue("White")] + public virtual Fill SelectedColor { + get { return selectedColor; } + set { + if (selectedColor == value) + return; + selectedColor = value; + NotifyValueChanged ("SelectedColor", selectedColor); + string n = (selectedColor as SolidColor).color.ToString (); + if (char.IsLetter(n[0])) + NotifyValueChanged ("SelectedColorName", n); + else + NotifyValueChanged ("SelectedColorName", "-"); + NotifyValueChanged ("R", R); + NotifyValueChanged ("G", G); + NotifyValueChanged ("B", B); + NotifyValueChanged ("A", A); + //NotifyValueChanged ("A", sc.color.A); + } + } + + public override void onMouseMove (object sender, MouseMoveEventArgs e) + { + base.onMouseMove (sender, e); + Rectangle r = ScreenCoordinates (Slot); + Point pos = e.Position - r.Position; + //System.Diagnostics.Debug.WriteLine ("{0} {1}", pos.X, pos.Y); + if (bmp == null) + return; + PointedColor = new SolidColor(getPixelAt(pos.X, pos.Y)); + } + + Color getPixelAt(int x, int y){ + int ptr = y * Slot.Width * 4 + x * 4; + + return new Color( + (double)bmp[ptr + 2] * colDiv, + (double)bmp[ptr + 1] * colDiv, + (double)bmp[ptr] * colDiv, + (double)bmp[ptr + 3] * colDiv); + +// return new Color( +// 1.0 / (double)bmp[ptr + 1], +// 1.0 / (double)bmp[ptr + 2], +// 1.0 / (double)bmp[ptr + 3], +// 1.0 / (double)bmp[ptr]); + } + + public override void onMouseClick (object sender, MouseButtonEventArgs e) + { + base.onMouseClick (sender, e); + SelectedColor = PointedColor; + } + } +} + diff --git a/src/GraphicObjects/GraphicObject.cs b/src/GraphicObjects/GraphicObject.cs index b58bf7eb..d182f54a 100644 --- a/src/GraphicObjects/GraphicObject.cs +++ b/src/GraphicObjects/GraphicObject.cs @@ -404,6 +404,8 @@ namespace Crow set { if (background == value) return; + if (value == null) + return; background = value; NotifyValueChanged ("Background", background); RegisterForRedraw (); diff --git a/src/SolidColor.cs b/src/SolidColor.cs index 2b6f7e71..d214f592 100644 --- a/src/SolidColor.cs +++ b/src/SolidColor.cs @@ -12,7 +12,7 @@ namespace Crow { public class SolidColor : Fill { - Color color = Color.Transparent; + public Color color = Color.Transparent; #region CTOR public SolidColor(Color c) { -- 2.47.3