From c3b559a9144300352379e8d80dd71131b45f4d99 Mon Sep 17 00:00:00 2001 From: HOSOKAWA Kenchi Date: Thu, 4 Oct 2012 14:14:16 +0900 Subject: [PATCH] da --- dev5/psychlops/core/graphic/image.cs | 23 ++- dev5/psychlops/extension/standard/CIEColor.cs | 39 ++++ dev5/psychlops/extension/standard/widget.cs | 5 +- test5/PsychlopsMain.cs | 265 ++++++++++++++++++++------ 4 files changed, 264 insertions(+), 68 deletions(-) diff --git a/dev5/psychlops/core/graphic/image.cs b/dev5/psychlops/core/graphic/image.cs index 64f3c1e..3bc98bf 100644 --- a/dev5/psychlops/core/graphic/image.cs +++ b/dev5/psychlops/core/graphic/image.cs @@ -77,6 +77,21 @@ namespace Psychlops{ buffer.SetPixel(x, y, col); } + public void pix_raw(int x, int y, Color col) + { + buffer.SetPixel(x, y, col); + } + + public void pix_direct(int x, int y, Color col) + { + buffer.SetPixel(x, y, col); + } + + public void pix_direct(int x, int y, double r, double g, double b, double a) + { + buffer.SetPixel(x, y, new Color(r,g,b,a)); + } + public void release() { } @@ -92,13 +107,7 @@ namespace Psychlops{ public void clear(Color col) { - for (int y = 0; y < height; y++) - { - for (int x = 0; x < width; x++) - { - pix(x, y, col); - } - } + each((x, y) => col); } diff --git a/dev5/psychlops/extension/standard/CIEColor.cs b/dev5/psychlops/extension/standard/CIEColor.cs index 50df53c..bf08e3a 100644 --- a/dev5/psychlops/extension/standard/CIEColor.cs +++ b/dev5/psychlops/extension/standard/CIEColor.cs @@ -4,6 +4,45 @@ namespace Psychlops { namespace ColorSpaces { + public struct HSV + { + public double H, S, V, A; + + public Color toRGB() + { + if (S == 0.0) return new Color(V, V, V, A); + int hi = (int)Math.floor(H/60.0); + double f = (H / 60.0) - (double)hi, + p = V*(1.0-S), + q = V*(1.0-f*S), + t = V*(1.0-(1.0-f)*S); + if (hi == 0) { return new Color(V, t, p, A); } + else if (hi == 1) { return new Color(q, V, p, A); } + else if (hi == 2) { return new Color(p, V, t, A); } + else if (hi == 3) { return new Color(p, q, V, A); } + else if (hi == 4) { return new Color(t, p, V, A); } + else if (hi == 5) { return new Color(V, p, q, A); } + else return Color.transparent; + } + + public void fromRGB(Color o) { + double MAX = Math.max(Math.max(o.r, o.g), o.b); + double MIN = Math.min(Math.min(o.r, o.g), o.b); + double h; + if(MAX==MIN) { h=0.0; } + else if(o.r>o.g && o.r>o.b) { h=60.0*(o.g-o.b)/(MAX-MIN)+360.0; } + else if(o.g>o.b) { h=60.0*(o.b-o.r)/(MAX-MIN)+120.0; } + else { h=60.0*(o.r-o.g)/(MAX-MIN)+240.0; } + h = Math.mod(h, 360.0); + double v = MAX, s; + if(MAX==MIN) { s=0.0; } else { s=(MAX-MIN)/MAX; } + H = h; + S = s; + V = v; + A = o.a; + } + } + /* * CIE 1931 * R: 700 nm diff --git a/dev5/psychlops/extension/standard/widget.cs b/dev5/psychlops/extension/standard/widget.cs index 51d6087..4f4cee5 100644 --- a/dev5/psychlops/extension/standard/widget.cs +++ b/dev5/psychlops/extension/standard/widget.cs @@ -149,11 +149,12 @@ namespace Psychlops static Slider() { - bgcolor.set(.5,.5, .5, .3); + bgcolor.set(.5, .5, .5, .3); } public Slider(string l, Interval r, double initialvalue) { + changed = true; retval = initialvalue; range = r; label = l; @@ -167,6 +168,7 @@ namespace Psychlops } public Slider(string l, Interval r, double step1, double step2) { + changed = true; retval = r.begin.value; range = r; label = l; @@ -341,6 +343,7 @@ namespace Psychlops slide.SmallChange = ss.small_step; } slide.ValueChanged += slider_ValueChanged; + slide.ValueChanged += (sender, e) => { ss.changed = true; }; System.Windows.Controls.Canvas.SetTop(slide, label.ActualHeight - 3); var b = new System.Windows.Data.Binding { Path = new PropertyPath("Value"), diff --git a/test5/PsychlopsMain.cs b/test5/PsychlopsMain.cs index 4242429..792f064 100644 --- a/test5/PsychlopsMain.cs +++ b/test5/PsychlopsMain.cs @@ -1,4 +1,113 @@ -// Motion with glass patterns. +/* +// Lilac Chaser. +// Zaidi Q, Ennis R, Cao D, Lee B (2012) +// Neural locus of color after-image. +// Current Biology, 22, 220-224 +///+ Prefix +//// Include Psychlops Package +using Psychlops; + +namespace PsychlopsSilverlightApp +{ + + public class PsychlopsMain + { +///- Prefix + + + ///+ Stimulus drawing function + //// A function for stimulus drawing (main body) + public void psychlops_main() { + + ///+ Preperation + //// Declare and initialize local variables + Canvas cnvs = new Canvas(Canvas.window); //Prepare drawing window + double dotsize = 100; + double dot_lum = 1; + double bg_lum = 0.5; + double duration = 8.0; + int stroke = 0; + bool draw_base_position = false; + + var fixate = new Rectangle(); + Image dot = new Image(); + Color col = new Color(); + double xp, yp, radius, a; + + Psychlops.Widgets.Slider hue, value; + ///- Preperation + + ///+ set Independent + //// set variables and value ranges for interaction + Interval rng = new Interval(); + hue = new Psychlops.Widgets.Slider("Hue", 0.0 <= rng < 360.0, 1, 12); + hue.setValue(300.0); + value = new Psychlops.Widgets.Slider("Lightness", 0 <= rng <= 1.0, 0.1, 0.05); + value.setValue(0.7); + ///- set Independent + + dot.set(dotsize, dotsize); + double width = dotsize, height = dotsize, sigma = (width / 6.0); + + fixate.set(11,11); + + int frame = 0; + + while (!Keyboard.esc.pushed()) + { + ///+ reflesh dot + if (hue.changed) + { + Psychlops.ColorSpaces.HSV hsv = new Psychlops.ColorSpaces.HSV(); + hsv.H = Math.mod( hue.getValue(), 360); + hsv.S = 1.0; + hsv.V = value.getValue(); + dot.each((x, y) => + { + yp = y - height / 2.0; + xp = x - width / 2.0; + radius = Math.sqrt(xp * xp + yp * yp); + hsv.A = Math.exp(-(radius * radius) / (2.0 * sigma * sigma)); + return hsv.toRGB(); + }); + } + ///+ reflesh dot + + + ///+ stroke count + if(frame%((int)duration)==0) { + stroke++; stroke %= 8; + } + ///- stroke count + + cnvs.clear(new Color(bg_lum)); //Clear window + + + ///+ Draw dots + //// Draw dots at a desinated position. + for(int i=0; i<8; i++) { + if(i!=stroke) { + dot.centering().shift(dotsize*1.3*Math.cos(i/8.0*2*Math.PI), dotsize*1.3*Math.sin(i/8.0*2*Math.PI)).draw(); + } + } + ///- Draw dots + + fixate.centering().draw(); + + cnvs.flip(); // Flip frame buffers + frame++; + } + + } + ///- Stimulus drawing function + + } + +} +*/ + + +// Motion with glass patterns. // Ross, J., Badcock, D. R., and Hayes, A. (2000) // Coherent global motion in the absence of coherent velocity signals. // Current Biology, 10, 679-682. @@ -29,11 +138,12 @@ namespace PsychlopsSilverlightApp //// Declare and initialize local variables Canvas cnvs = new Canvas(400,400); //Prepare drawing window double dotsize = 2; - double dot_lum = 0.5; + double dot_lum = 1.0; double bg_lum = 0.2; double fieldsize = 300; double[] orientation = new double[DOTNUM]; Rectangle dots = new Rectangle(dotsize, dotsize); + Ellipse dot = new Ellipse(dotsize*3, dotsize*3); ///+ prepare dots position //// prepare dots position and paired orientation Matrix positionmat = Matrix.gen(DOTNUM,2); @@ -54,7 +164,7 @@ namespace PsychlopsSilverlightApp _x=i-0.5*fieldsize-50; for(int j=0; j duration) + int stroke = 0; + bool draw_base_position = false; + Color col = new Color(); + + + ///+ initialize position + //// Re-randomize position with a desingated interval + //Math.random(positionmat, -0.5 * fieldsize, 0.5 * fieldsize); + positionmat.each((v) => Math.random(-0.5 * fieldsize, 0.5 * fieldsize)); + for (int i = 0; i < DOTNUM; i++) { orientation[i] = Math.atan2(positionmat[i + 1, 2], positionmat[i + 1, 1]); } + frame = 0; + ///- initialize position + + while (!Keyboard.esc.pushed()) + { + ///+ stroke count + if (frame % duration == 0) { - //Math.random(positionmat, -0.5 * fieldsize, 0.5 * fieldsize); - positionmat.each((v) => Math.random(-0.5 * fieldsize, 0.5 * fieldsize)); - for (int i = 0; i < DOTNUM; i++) { orientation[i] = Math.atan2(positionmat[i + 1, 2], positionmat[i + 1, 1]); } - frame = 0; + stroke++; stroke %= 4; + if (stroke < 2) col.set(dot_lum); + else col.set(1 - dot_lum); + draw_base_position = (stroke % 2 == 0); } - ///- position change + ///- stroke count + Display.clear(new Color(bg_lum)); //Clear window - //// Draw DOTNUM pairs of dots - for(int i=0; i