<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" />
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
{
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;
}
[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;
}
[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;
}
[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;
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; }
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(
(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;
- }
}
}
--- /dev/null
+//
+// 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 ();
+ }
+ }
+}
+
--- /dev/null
+//
+// 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 ();
+ }
+ }
+}
+