From: jpbruyere Date: Fri, 21 Oct 2016 04:18:21 +0000 (+0200) Subject: debug X-Git-Tag: v0.5.1~28^2~16 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=169f569ca2356cdc5cbe9939323dda4478aab07e;p=jp%2Fcrow.git debug modifié : Templates/ColorPicker.template modifié : Tests/Interfaces/Divers/colorPicker.crow copié : src/GraphicObjects/ColorSelector.cs -> src/GraphicObjects/ColorPicker.cs modifié : src/GraphicObjects/ColorSelector.cs modifié : src/GraphicObjects/HueSelector.cs modifié : src/GraphicObjects/SaturationValueSelector.cs modifié : src/Colors.cs --- diff --git a/Templates/ColorPicker.template b/Templates/ColorPicker.template index 4a542631..5bf21131 100755 --- a/Templates/ColorPicker.template +++ b/Templates/ColorPicker.template @@ -3,46 +3,46 @@ CornerRadius="{./CornerRadius}" BorderWidth="1"> - - + + - + - diff --git a/Tests/Interfaces/Divers/colorPicker.crow b/Tests/Interfaces/Divers/colorPicker.crow index add58965..17700afb 100755 --- a/Tests/Interfaces/Divers/colorPicker.crow +++ b/Tests/Interfaces/Divers/colorPicker.crow @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/src/Colors.cs b/src/Colors.cs index 6787c382..b76de623 100644 --- a/src/Colors.cs +++ b/src/Colors.cs @@ -1071,5 +1071,34 @@ namespace Crow { return (Color)s; } + public static Color FromHSV(double _h, double _v = 1.0, double _s = 1.0){ + Color c = Color.Black; + + if (_s == 0) {//HSV from 0 to 1 + c.R = _v; + c.G = _v; + c.B = _v; + }else{ + double var_h = _h * 6.0; + + if (var_h == 6.0) + var_h = 0; //H must be < 1 + double var_i = Math.Floor( var_h ); //Or ... var_i = floor( var_h ) + double var_1 = _v * ( 1.0 - _s ); + double var_2 = _v * (1.0 - _s * (var_h - var_i)); + double var_3 = _v * (1.0 - _s * (1.0 - (var_h - var_i))); + + if (var_i == 0.0) { + c.R = _v; + c.G = var_3; + c.B = var_1; + }else if ( var_i == 1.0 ) { c.R = var_2 ; c.G = _v ; c.B = var_1; } + else if ( var_i == 2 ) { c.R = var_1 ; c.G = _v ; c.B = var_3; } + else if ( var_i == 3 ) { c.R = var_1 ; c.G = var_2 ; c.B = _v; } + else if ( var_i == 4 ) { c.R = var_3 ; c.G = var_1 ; c.B = _v; } + else { c.R = _v ; c.G = var_1 ; c.B = var_2; } + } + return c; + } } } diff --git a/src/GraphicObjects/ColorPicker.cs b/src/GraphicObjects/ColorPicker.cs index b95fb0e3..cc8dfac3 100644 --- a/src/GraphicObjects/ColorPicker.cs +++ b/src/GraphicObjects/ColorPicker.cs @@ -30,17 +30,193 @@ namespace Crow { } - Fill selectedColor; + const double div = 255.0; + const double colDiv = 1.0 / div; + + Color curColor; + double h,s,v; + + [XmlAttributeAttribute()] + public virtual double R { + get { return Math.Round(curColor.R * div); } + set { + if (R == value) + return; + curColor.R = value * colDiv; + NotifyValueChanged ("R", R); + hsvFromRGB (); + notifyCurColorHasChanged (); + } + } + [XmlAttributeAttribute()] + public virtual double G { + get { return Math.Round(curColor.G * div); } + set { + if (G == value) + return; + curColor.G = value * colDiv; + NotifyValueChanged ("G", G); + notifyCurColorHasChanged (); + hsvFromRGB (); + } + } + [XmlAttributeAttribute()] + public virtual double B { + get { return Math.Round(curColor.B * div); } + set { + if (B == value) + return; + curColor.B = value * colDiv; + NotifyValueChanged ("B", B); + notifyCurColorHasChanged (); + hsvFromRGB (); + } + } + [XmlAttributeAttribute()] + public virtual double A { + get { return Math.Round(curColor.A * div); } + set { + if (A == value) + return; + curColor.A = value * colDiv; + NotifyValueChanged ("A", A); + notifyCurColorHasChanged (); + hsvFromRGB (); + } + } + [XmlAttributeAttribute()] + public virtual double H { + get { return Math.Round (h, 3); } + set { + if (H == value) + return; + h = value; + NotifyValueChanged ("H", H); + rgbFromHSV (); + } + } + [XmlAttributeAttribute()] + public virtual double S { + get { return Math.Round (s, 2); } + set { + if (s == value) + return; + s = value; + NotifyValueChanged ("S", S); + rgbFromHSV (); + } + } + [XmlAttributeAttribute()] + public virtual double V { + get { return Math.Round (v, 2); } + set { + if (v == value) + return; + v = value; + NotifyValueChanged ("V", V); + rgbFromHSV (); + } + } [XmlAttributeAttribute] public virtual Fill SelectedColor { - get { return selectedColor; } + get { return new SolidColor(curColor); } set { - if (selectedColor == value) + Color c = (value as SolidColor).color; + if (curColor == c) return; - selectedColor = value; - NotifyValueChanged ("SelectedColor", selectedColor); + curColor = c; + notifyCurColorHasChanged (); + notifyRGBAHasChanged (); + hsvFromRGB (); + } + } + + void notifyCurColorHasChanged(){ + NotifyValueChanged ("SelectedColor", SelectedColor); + string n = curColor.ToString (); + if (char.IsLetter(n[0])) + NotifyValueChanged ("SelectedColorName", n); + else + NotifyValueChanged ("SelectedColorName", "-"); + NotifyValueChanged ("HexColor", ((int)R).ToString ("X2") + ((int)G).ToString ("X2") + ((int)B).ToString ("X2") + ((int)A).ToString ("X2")); + } + void notifyRGBAHasChanged(){ + NotifyValueChanged ("R", R); + NotifyValueChanged ("G", G); + NotifyValueChanged ("B", B); + NotifyValueChanged ("A", A); + } + void notifyHSVHasChanged(){ + NotifyValueChanged ("H", H); + NotifyValueChanged ("S", S); + NotifyValueChanged ("V", V); + } + void hsvFromRGB(){ + Color c = curColor; + double min = Math.Min (c.R, Math.Min (c.G, c.B)); //Min. value of RGB + double max = Math.Max (c.R, Math.Max (c.G, c.B)); //Max. value of RGB + double diff = max - min; //Delta RGB value + + v = max; + + if ( diff == 0 )//This is a gray, no chroma... + { + h = 0; + s = 0; + }else{//Chromatic data... + s = diff / max; + + double diffR = (((max - c.R) / 6.0) + (diff / 2.0)) / diff; + double diffG = (((max - c.G) / 6.0) + (diff / 2.0)) / diff; + double diffB = (((max - c.B) / 6.0) + (diff / 2.0)) / diff; + + if (c.R == max) + h = diffB - diffG; + else if (c.G == max) + h = (1.0 / 3.0) + diffR - diffB; + else if (c.B == max) + h = (2.0 / 3.0) + diffG - diffR; + + if (h < 0) + h += 1; + if (h > 1) + h -= 1; + + } + notifyHSVHasChanged (); + } + void rgbFromHSV(){ + Color c = Color.Black; + + if (s == 0) {//HSV from 0 to 1 + c.R = v; + c.G = v; + c.B = v; + }else{ + double var_h = h * 6.0; + + if (var_h == 6.0) + var_h = 0; //H must be < 1 + double var_i = Math.Floor( var_h ); //Or ... var_i = floor( var_h ) + double var_1 = v * ( 1.0 - s ); + double var_2 = v * (1.0 - s * (var_h - var_i)); + double var_3 = v * (1.0 - s * (1.0 - (var_h - var_i))); + + if (var_i == 0.0) { + c.R = v; + c.G = var_3; + c.B = var_1; + }else if ( var_i == 1.0 ) { c.R = var_2 ; c.G = v ; c.B = var_1; } + else if ( var_i == 2 ) { c.R = var_1 ; c.G = v ; c.B = var_3; } + else if ( var_i == 3 ) { c.R = var_1 ; c.G = var_2 ; c.B = v; } + else if ( var_i == 4 ) { c.R = var_3 ; c.G = var_1 ; c.B = v; } + else { c.R = v ; c.G = var_1 ; c.B = var_2; } } + + curColor = c; + notifyCurColorHasChanged (); + notifyRGBAHasChanged (); } } } diff --git a/src/GraphicObjects/ColorSelector.cs b/src/GraphicObjects/ColorSelector.cs index 39db4f25..13a588ce 100644 --- a/src/GraphicObjects/ColorSelector.cs +++ b/src/GraphicObjects/ColorSelector.cs @@ -32,127 +32,23 @@ namespace Crow const double div = 255.0; const double colDiv = 1.0 / div; - - Fill selectedColor = new SolidColor(Color.Red); protected Point mousePos; - double h,s,v; - - [XmlAttributeAttribute()] - public virtual double R { - get { return Math.Round((selectedColor as SolidColor).color.R * div); } - set { - if (R == value) - return; - Color c = (SelectedColor as SolidColor).color; - SelectedColor = new SolidColor (new Color (value * colDiv, c.G, c.B, c.A)); - NotifyValueChanged ("R", R); - updateHSV (); - } - } - [XmlAttributeAttribute()] - public virtual double G { - get { return Math.Round((selectedColor as SolidColor).color.G * div); } - set { - if (G == value) - return; - Color c = (SelectedColor as SolidColor).color; - SelectedColor = new SolidColor (new Color (c.R, value * colDiv, c.B, c.A)); - NotifyValueChanged ("G", G); - updateHSV (); - } - } - [XmlAttributeAttribute()] - public virtual double B { - get { return Math.Round((selectedColor as SolidColor).color.B * div); } - set { - if (B == value) - return; - Color c = (SelectedColor as SolidColor).color; - SelectedColor = new SolidColor (new Color (c.R, c.G, value * colDiv, c.A)); - NotifyValueChanged ("B", B); - updateHSV (); - } - } - [XmlAttributeAttribute()] - public virtual double A { - get { return Math.Round((selectedColor as SolidColor).color.A * div); } - set { - if (A == value) - return; - Color c = (SelectedColor as SolidColor).color; - SelectedColor = new SolidColor (new Color (c.R, c.G, c.B, value * colDiv)); - NotifyValueChanged ("A", A); - updateHSV (); - } - } - [XmlAttributeAttribute()] - public virtual double H { - get { return Math.Round (h, 3); } - set { - if (H == value) - return; - h = value; - NotifyValueChanged ("H", H); - computeColorFromHSV (); - } - } - [XmlAttributeAttribute()] - public virtual double S { - get { return Math.Round (s, 2); } - set { - if (s == value) - return; - s = value; - NotifyValueChanged ("S", S); - computeColorFromHSV (); - } - } - [XmlAttributeAttribute()] - public virtual double V { - get { return Math.Round (v, 2); } - set { - if (v == value) - return; - v = value; - NotifyValueChanged ("V", V); - computeColorFromHSV (); - } - } - - [XmlAttributeAttribute] - 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 ("HexColor", ((int)R).ToString ("X") + ((int)G).ToString ("X") + ((int)B).ToString ("X") + ((int)A).ToString ("X")); - } - } public override void onMouseMove (object sender, MouseMoveEventArgs e) { base.onMouseMove (sender, e); - if (bmp == null || CurrentInterface.Mouse.LeftButton == ButtonState.Released) + if (CurrentInterface.Mouse.LeftButton == ButtonState.Released) return; updateMouseLocalPos (e.Position); - updateColorFromPicking (); } - - public override void onMouseClick (object sender, MouseButtonEventArgs e) + public override void onMouseDown (object sender, MouseButtonEventArgs e) { - base.onMouseClick (sender, e); - - updateMouseLocalPos (e.Position); - updateColorFromPicking (); + base.onMouseDown (sender, e); + if (e.Button == MouseButton.Left) + updateMouseLocalPos (e.Position); } - void updateMouseLocalPos(Point mPos){ + + protected virtual void updateMouseLocalPos(Point mPos){ Rectangle r = ScreenCoordinates (Slot); Rectangle cb = ClientRectangle; mousePos = mPos - r.Position; @@ -162,104 +58,32 @@ namespace Crow mousePos.Y = Math.Max(cb.Y, mousePos.Y); mousePos.Y = Math.Min(cb.Bottom-1, mousePos.Y); } - virtual protected void updateColorFromPicking(bool redraw = true){ - SelectedColor = new SolidColor(getPixelAt(mousePos.X, mousePos.Y)); - - updateHSV (); - - NotifyValueChanged ("R", R); - NotifyValueChanged ("G", G); - NotifyValueChanged ("B", B); - NotifyValueChanged ("A", A); - - if (redraw) - 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 + 2] * colDiv, - (double)bmp[ptr + 1] * colDiv, - (double)bmp[ptr] * colDiv, - (double)bmp[ptr + 3] * colDiv); - } - void updateHSV(){ - Color c = (SelectedColor as SolidColor).color; - double min = Math.Min (c.R, Math.Min (c.G, c.B)); //Min. value of RGB - double max = Math.Max (c.R, Math.Max (c.G, c.B)); //Max. value of RGB - double diff = max - min; //Delta RGB value - - v = max; - - if ( diff == 0 )//This is a gray, no chroma... - { - h = 0; - s = 0; - }else{//Chromatic data... - s = diff / max; - - double diffR = (((max - c.R) / 6.0) + (diff / 2.0)) / diff; - double diffG = (((max - c.G) / 6.0) + (diff / 2.0)) / diff; - double diffB = (((max - c.B) / 6.0) + (diff / 2.0)) / diff; - - if (c.R == max) - h = diffB - diffG; - else if (c.G == max) - h = (1.0 / 3.0) + diffR - diffB; - else if (c.B == max) - h = (2.0 / 3.0) + diffG - diffR; - - if (h < 0) - h += 1; - if (h > 1) - h -= 1; - - } - - NotifyValueChanged ("H", H); - NotifyValueChanged ("S", S); - NotifyValueChanged ("V", V); - } - void computeColorFromHSV(){ - Color c = Color.Black; - - if (s == 0) {//HSV from 0 to 1 - c.R = v; - c.G = v; - c.B = v; - }else{ - double var_h = h * 6.0; - - if (var_h == 6.0) - var_h = 0; //H must be < 1 - double var_i = Math.Floor( var_h ); //Or ... var_i = floor( var_h ) - double var_1 = v * ( 1.0 - s ); - double var_2 = v * (1.0 - s * (var_h - var_i)); - double var_3 = v * (1.0 - s * (1.0 - (var_h - var_i))); - - if (var_i == 0.0) { - c.R = v; - c.G = var_3; - c.B = var_1; - }else if ( var_i == 1.0 ) { c.R = var_2 ; c.G = v ; c.B = var_1; } - else if ( var_i == 2 ) { c.R = var_1 ; c.G = v ; c.B = var_3; } - else if ( var_i == 3 ) { c.R = var_1 ; c.G = var_2 ; c.B = v; } - else if ( var_i == 4 ) { c.R = var_3 ; c.G = var_1 ; c.B = v; } - else { c.R = v ; c.G = var_1 ; c.B = var_2; } - } - - SelectedColor = new SolidColor (c); - - NotifyValueChanged ("R", R); - NotifyValueChanged ("G", G); - NotifyValueChanged ("B", B); - NotifyValueChanged ("A", A); - } +// virtual protected void updateColorFromPicking(bool redraw = true){ +// SelectedColor = new SolidColor(getPixelAt(mousePos.X, mousePos.Y)); +// +// updateHSV (); +// +// NotifyValueChanged ("R", R); +// NotifyValueChanged ("G", G); +// NotifyValueChanged ("B", B); +// NotifyValueChanged ("A", A); +// +// if (redraw) +// 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 + 2] * colDiv, +// (double)bmp[ptr + 1] * colDiv, +// (double)bmp[ptr] * colDiv, +// (double)bmp[ptr + 3] * colDiv); +// } } } diff --git a/src/GraphicObjects/HueSelector.cs b/src/GraphicObjects/HueSelector.cs index 0ec92c10..b291c4c9 100644 --- a/src/GraphicObjects/HueSelector.cs +++ b/src/GraphicObjects/HueSelector.cs @@ -32,14 +32,32 @@ namespace Crow } Orientation _orientation; + double hue; [XmlAttributeAttribute][DefaultValue(Orientation.Horizontal)] public virtual Orientation Orientation { get { return _orientation; } - set { _orientation = value; } + set { + if (_orientation == value) + return; + _orientation = value; + NotifyValueChanged ("Orientation", _orientation); + RegisterForGraphicUpdate (); + } } + [XmlAttributeAttribute()] + public virtual double Hue { + get { return hue; } + set { + if (hue == value) + return; + hue = value; + notifyHueChanged (); + updateMousePosFromHue (); + } + } protected override void onDraw (Cairo.Context gr) { base.onDraw (gr); @@ -90,6 +108,26 @@ namespace Crow ctx.Stroke(); ctx.Restore (); } + protected override void updateMouseLocalPos (Point mPos) + { + base.updateMouseLocalPos (mPos); + if (Orientation == Orientation.Horizontal) + hue = (double)mousePos.X / (double)ClientRectangle.Width; + else + hue = (double)mousePos.Y / (double)ClientRectangle.Height; + notifyHueChanged (); + } + void updateMousePosFromHue(){ + if (Orientation == Orientation.Horizontal) + mousePos.X = (int)Math.Floor(hue * (double)ClientRectangle.Width); + else + mousePos.Y = (int)Math.Floor(hue * (double)ClientRectangle.Height); + CurrentInterface.EnqueueForRepaint (this); + } + void notifyHueChanged(){ + NotifyValueChanged ("Hue", hue); + NotifyValueChanged ("HueColor", new SolidColor (Color.FromHSV (hue))); + } } } diff --git a/src/GraphicObjects/SaturationValueSelector.cs b/src/GraphicObjects/SaturationValueSelector.cs index 8d6b4228..b8a5bafd 100644 --- a/src/GraphicObjects/SaturationValueSelector.cs +++ b/src/GraphicObjects/SaturationValueSelector.cs @@ -20,6 +20,7 @@ // along with this program. If not, see . using System; using Cairo; +using System.Xml.Serialization; namespace Crow { @@ -29,6 +30,33 @@ namespace Crow { } + double v, s; + + [XmlAttributeAttribute()] + public virtual double V { + get { return v; } + set { + if (v == value) + return; + v = value; + NotifyValueChanged ("V", v); + mousePos.Y = (int)Math.Floor((1.0-v) * (double)ClientRectangle.Height); + + CurrentInterface.EnqueueForRepaint (this); + } + } + [XmlAttributeAttribute()] + public virtual double S { + get { return s; } + set { + if (s == value) + return; + s = value; + NotifyValueChanged ("S", s); + mousePos.X = (int)Math.Floor(s * (double)ClientRectangle.Width); + CurrentInterface.EnqueueForRepaint (this); + } + } protected override void onDraw (Cairo.Context gr) { base.onDraw (gr); @@ -53,9 +81,8 @@ namespace Crow grad.SetAsSource (gr, rGrad); CairoHelpers.CairoRectangle (gr, r, CornerRadius); gr.Fill(); - - updateColorFromPicking (false); } + public override void Paint (ref Context ctx) { base.Paint (ref ctx); @@ -72,6 +99,17 @@ namespace Crow ctx.Restore (); } + + protected override void updateMouseLocalPos (Point mPos) + { + base.updateMouseLocalPos (mPos); + + Rectangle cb = ClientRectangle; + s = (double)mousePos.X / (double)cb.Width; + v = 1.0 - (double)mousePos.Y / (double)cb.Height; + NotifyValueChanged ("S", s); + NotifyValueChanged ("V", v); + } } }