]> O.S.I.I.S - jp/crow.git/commitdiff
Hue selector and Saturation/value selector
authorjpbruyere <jp.bruyere@hotmail.com>
Thu, 20 Oct 2016 06:41:00 +0000 (08:41 +0200)
committerjpbruyere <jp.bruyere@hotmail.com>
Thu, 20 Oct 2016 06:41:00 +0000 (08:41 +0200)
modifié :         Crow.csproj
modifié :         Templates/ColorPicker.template
modifié :         src/GraphicObjects/ColorSelector.cs
nouveau fichier : src/GraphicObjects/HueSelector.cs
nouveau fichier : src/GraphicObjects/SaturationValueSelector.cs

Crow.csproj
Templates/ColorPicker.template
src/GraphicObjects/ColorSelector.cs
src/GraphicObjects/HueSelector.cs [new file with mode: 0644]
src/GraphicObjects/SaturationValueSelector.cs [new file with mode: 0644]

index d34bcc3684612375366d13f8e7e0667ba087dc03..b2be9a5b816d113a2abe13514ca6e0fa86c2951e 100644 (file)
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="src\GraphicObjects\ColorPicker.cs" />
     <Compile Include="src\GraphicObjects\ColorSelector.cs" />
+    <Compile Include="src\GraphicObjects\HueSelector.cs" />
+    <Compile Include="src\GraphicObjects\SaturationValueSelector.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="System" />
index ececc7ac13f3fa8ef8124387a41dac3b38318221..c4fecf76d1e179eda0350b574c06e93bde706fed 100755 (executable)
@@ -3,29 +3,31 @@
                CornerRadius="{./CornerRadius}" BorderWidth="1">
                <HorizontalStack Spacing="0">
                        <VerticalStack Margin="5">
-                               <ColorSelector Focusable="true" Name="colorSelector" Margin="1" Width="200" Height="200" Background="DimGray"/>
+                               <SaturationValueSelector Focusable="true" Name="colorSelector" Margin="0" Width="200" Height="200" Background="DimGray"
+                                       Foreground="{../hueSelector.SelectedColor}"/>
+                               <HueSelector Focusable="true" Name="hueSelector" Margin="0" Width="200" Height="20" Background="DimGray"/>
                        </VerticalStack>
-                       <VerticalStack Margin="5">
-                               <HorizontalStack>
+                       <VerticalStack Margin="5" Height="Stretched">
+                               <HorizontalStack Height="Fit" Width="Stretched">
+                                       <GraphicObject Width="40" Height="40" Background="{../../../colorSelector.SelectedColor}"/>
+                                       <Label Width="Stretched" Background="Jet" Text="{../../../colorSelector.SelectedColorName}" TextAlignment="Center"/>
+                               </HorizontalStack>
+                               <HorizontalStack Height="Fit">
                                        <Label Text="Red:" Width="50"/>
                                        <Spinner Style="ColorSpinner" Value="{²../../../colorSelector.R}" Width="50"  />
                                </HorizontalStack>
-                               <HorizontalStack>
+                               <HorizontalStack Height="Fit">
                                        <Label Text="Green:" Width="50"/>
                                        <Spinner Style="ColorSpinner" Value="{²../../../colorSelector.G}" Width="50"  />
                                </HorizontalStack>
-                               <HorizontalStack>
+                               <HorizontalStack Height="Fit">
                                        <Label Text="Blue:" Width="50"/>
                                        <Spinner Style="ColorSpinner" Value="{²../../../colorSelector.B}" Width="50"  />
                                </HorizontalStack>
-                               <HorizontalStack>
+                               <HorizontalStack Height="Fit">
                                        <Label Text="Alpha:" Width="50"/>
                                        <Spinner Style="ColorSpinner" Value="{²../../../colorSelector.A}" Width="50"  />
                                </HorizontalStack>
-                               <GraphicObject Width="30" Height="20" Background="{../../colorSelector.PointedColor}"/>
-                               <Label Background="Jet" Text="{../../colorSelector.PointedColorName}"/>
-                               <GraphicObject Width="30" Height="20" Background="{../../colorSelector.SelectedColor}"/>
-                               <Label Background="Jet" Text="{../../colorSelector.SelectedColorName}"/>
                        </VerticalStack>
                </HorizontalStack>
 </Border>
\ No newline at end of file
index a5b9d042a1b005eeeec109a1158ca9a3eab5d525..e732a33aa751ecdfcf38f145f97035d0d41eee63 100644 (file)
@@ -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 (file)
index 0000000..0ec92c1
--- /dev/null
@@ -0,0 +1,95 @@
+//
+//  HueSelector.cs
+//
+//  Author:
+//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
+//
+//  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 <http://www.gnu.org/licenses/>.
+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 (file)
index 0000000..28990ee
--- /dev/null
@@ -0,0 +1,77 @@
+//
+//  SaturationValueSelector.cs
+//
+//  Author:
+//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
+//
+//  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 <http://www.gnu.org/licenses/>.
+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 ();
+               }
+       }
+}
+