From 7bd40c311b244245d3f2e0ae742390389a2e885a Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Thu, 20 Oct 2016 08:41:00 +0200 Subject: [PATCH] =?utf8?q?Hue=20selector=20and=20Saturation/value=20select?= =?utf8?q?or=20=09modifi=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20Crow.cspro?= =?utf8?q?j=20=09modifi=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20Templates/C?= =?utf8?q?olorPicker.template=20=09modifi=C3=A9=C2=A0:=20=20=20=20=20=20?= =?utf8?q?=20=20=20src/GraphicObjects/ColorSelector.cs=20=09nouveau=20fich?= =?utf8?q?ier=C2=A0:=20src/GraphicObjects/HueSelector.cs=20=09nouveau=20fi?= =?utf8?q?chier=C2=A0:=20src/GraphicObjects/SaturationValueSelector.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Crow.csproj | 2 + Templates/ColorPicker.template | 22 ++-- src/GraphicObjects/ColorSelector.cs | 102 +++++++----------- src/GraphicObjects/HueSelector.cs | 95 ++++++++++++++++ src/GraphicObjects/SaturationValueSelector.cs | 77 +++++++++++++ 5 files changed, 225 insertions(+), 73 deletions(-) create mode 100644 src/GraphicObjects/HueSelector.cs create mode 100644 src/GraphicObjects/SaturationValueSelector.cs diff --git a/Crow.csproj b/Crow.csproj index d34bcc36..b2be9a5b 100644 --- a/Crow.csproj +++ b/Crow.csproj @@ -148,6 +148,8 @@ + + diff --git a/Templates/ColorPicker.template b/Templates/ColorPicker.template index ececc7ac..c4fecf76 100755 --- a/Templates/ColorPicker.template +++ b/Templates/ColorPicker.template @@ -3,29 +3,31 @@ CornerRadius="{./CornerRadius}" BorderWidth="1"> - + + - - + + + + + - + - + - + - - \ No newline at end of file diff --git a/src/GraphicObjects/ColorSelector.cs b/src/GraphicObjects/ColorSelector.cs index a5b9d042..e732a33a 100644 --- a/src/GraphicObjects/ColorSelector.cs +++ b/src/GraphicObjects/ColorSelector.cs @@ -26,52 +26,19 @@ namespace Crow { public class ColorSelector : GraphicObject { - const double colDiv = 1.0 / 255.0; - - public ColorSelector ():base() + 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); + const double div = 255.0; + const double colDiv = 1.0 / div; - 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))); + Fill pointedColor, selectedColor; + protected Point mousePos; - 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); } + get { return Math.Round((selectedColor as SolidColor).color.R * div); } set { if (R == value) return; @@ -81,7 +48,7 @@ namespace Crow } [XmlAttributeAttribute()] public virtual double G { - get { return Math.Ceiling((selectedColor as SolidColor).color.G * 255.0); } + get { return Math.Round((selectedColor as SolidColor).color.G * div); } set { if (G == value) return; @@ -91,7 +58,7 @@ namespace Crow } [XmlAttributeAttribute()] public virtual double B { - get { return Math.Ceiling((selectedColor as SolidColor).color.B * 255.0); } + get { return Math.Round((selectedColor as SolidColor).color.B * div); } set { if (B == value) return; @@ -101,7 +68,7 @@ namespace Crow } [XmlAttributeAttribute()] public virtual double A { - get { return Math.Ceiling((selectedColor as SolidColor).color.A * 255.0); } + get { return Math.Round((selectedColor as SolidColor).color.A * div); } set { if (A == value) return; @@ -109,8 +76,6 @@ namespace Crow 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; } @@ -143,22 +108,44 @@ namespace Crow 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) + if (bmp == null || CurrentInterface.Mouse.LeftButton == ButtonState.Released) return; - PointedColor = new SolidColor(getPixelAt(pos.X, pos.Y)); + updateMouseLocalPos (e.Position); + updateColor (); } - Color getPixelAt(int x, int y){ + public override void onMouseClick (object sender, MouseButtonEventArgs e) + { + base.onMouseClick (sender, e); + + updateMouseLocalPos (e.Position); + updateColor (); + } + void updateMouseLocalPos(Point mPos){ + Rectangle r = ScreenCoordinates (Slot); + Rectangle cb = ClientRectangle; + mousePos = mPos - r.Position; + + mousePos.X = Math.Max(cb.X, mousePos.X); + mousePos.X = Math.Min(cb.Right-1, mousePos.X); + mousePos.Y = Math.Max(cb.Y, mousePos.Y); + mousePos.Y = Math.Min(cb.Bottom-1, mousePos.Y); + } + virtual protected void updateColor(){ + SelectedColor = new SolidColor(getPixelAt(mousePos.X, mousePos.Y)); + RegisterForRedraw (); + } + + protected Color getPixelAt(int x, int y){ + if (bmp == null) + return Color.Transparent; + int ptr = y * Slot.Width * 4 + x * 4; return new Color( @@ -166,19 +153,8 @@ namespace Crow (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/HueSelector.cs b/src/GraphicObjects/HueSelector.cs new file mode 100644 index 00000000..0ec92c10 --- /dev/null +++ b/src/GraphicObjects/HueSelector.cs @@ -0,0 +1,95 @@ +// +// HueSelector.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; +using Cairo; + +namespace Crow +{ + public class HueSelector : ColorSelector + { + public HueSelector () : base() + { + } + + Orientation _orientation; + + [XmlAttributeAttribute][DefaultValue(Orientation.Horizontal)] + public virtual Orientation Orientation + { + get { return _orientation; } + set { _orientation = value; } + } + + protected override void onDraw (Cairo.Context gr) + { + base.onDraw (gr); + + Rectangle r = ClientRectangle; + + Gradient.Type gt = Gradient.Type.Horizontal; + if (Orientation == Orientation.Vertical) + gt = Gradient.Type.Vertical; + + Crow.Gradient grad = new Gradient (gt); + + grad.Stops.Add (new Gradient.ColorStop (0, new Color (1, 0, 0, 1))); + grad.Stops.Add (new Gradient.ColorStop (0.167, new Color (1, 1, 0, 1))); + grad.Stops.Add (new Gradient.ColorStop (0.333, new Color (0, 1, 0, 1))); + grad.Stops.Add (new Gradient.ColorStop (0.5, new Color (0, 1, 1, 1))); + grad.Stops.Add (new Gradient.ColorStop (0.667, new Color (0, 0, 1, 1))); + grad.Stops.Add (new Gradient.ColorStop (0.833, new Color (1, 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(); + } + + public override void Paint (ref Context ctx) + { + base.Paint (ref ctx); + + Rectangle rb = Slot + Parent.ClientRectangle.Position; + ctx.Save (); + + ctx.Translate (rb.X, rb.Y); + + ctx.SetSourceColor (Color.White); + Rectangle r = ClientRectangle; + if (Orientation == Orientation.Horizontal) { + r.Width = 5; + r.X = mousePos.X; + } else { + r.Height = 5; + r.Y = mousePos.Y; + } + + CairoHelpers.CairoRectangle (ctx, r, 2); + ctx.SetSourceColor (Color.White); + ctx.LineWidth = 1.0; + ctx.Stroke(); + ctx.Restore (); + } + } +} + diff --git a/src/GraphicObjects/SaturationValueSelector.cs b/src/GraphicObjects/SaturationValueSelector.cs new file mode 100644 index 00000000..28990ee0 --- /dev/null +++ b/src/GraphicObjects/SaturationValueSelector.cs @@ -0,0 +1,77 @@ +// +// SaturationValueSelector.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 Cairo; + +namespace Crow +{ + public class SaturationValueSelector : ColorSelector + { + public SaturationValueSelector () : base() + { + } + + protected override void onDraw (Cairo.Context gr) + { + base.onDraw (gr); + + Rectangle r = ClientRectangle; + Rectangle rGrad = r; + rGrad.Inflate (-1); + + Foreground.SetAsSource (gr, r); + CairoHelpers.CairoRectangle (gr, r, CornerRadius); + gr.Fill(); + + Crow.Gradient grad = new Gradient (Gradient.Type.Horizontal); + grad.Stops.Add (new Gradient.ColorStop (0, new Color (1, 1, 1, 1))); + grad.Stops.Add (new Gradient.ColorStop (1, new Color (1, 1, 1, 0))); + grad.SetAsSource (gr, rGrad); + CairoHelpers.CairoRectangle (gr, r, CornerRadius); + gr.Fill(); + grad = new Gradient (Gradient.Type.Vertical); + grad.Stops.Add (new Gradient.ColorStop (0, new Color (0, 0, 0, 0))); + grad.Stops.Add (new Gradient.ColorStop (1, new Color (0, 0, 0, 1))); + grad.SetAsSource (gr, rGrad); + CairoHelpers.CairoRectangle (gr, r, CornerRadius); + gr.Fill(); + + SelectedColor = new SolidColor(getPixelAt(mousePos.X, mousePos.Y)); + } + public override void Paint (ref Context ctx) + { + base.Paint (ref ctx); + + Rectangle rb = Slot + Parent.ClientRectangle.Position; + ctx.Save (); + + ctx.Translate (rb.X, rb.Y); + + ctx.SetSourceColor (Color.White); + ctx.Arc (mousePos.X, mousePos.Y, 3.0, 0, Math.PI * 2.0); + ctx.LineWidth = 1.0; + ctx.Stroke (); + + ctx.Restore (); + } + } +} + -- 2.47.3