Пример #1
0
        public void GetInterpolatedColor()
        {
            var defaultColorScale = new ColorScale(new List <Color>()
            {
                Colors.Cyan, Colors.Purple, Colors.Orange
            });

            Assert.Equal(defaultColorScale.Colors[1], defaultColorScale.GetColor(0.5));
            Assert.Equal(defaultColorScale.Colors[0], defaultColorScale.GetColor(0.0));
            Assert.Equal(defaultColorScale.Colors[2], defaultColorScale.GetColor(1.0));

            var unevenColorScale = new ColorScale(new List <Color>()
            {
                Colors.Cyan, Colors.Purple, Colors.Orange
            }, new List <double>()
            {
                0, 10, 15
            });

            Assert.Equal(unevenColorScale.Colors[0], unevenColorScale.GetColor(0.0));
            Assert.Equal(unevenColorScale.Colors[1], unevenColorScale.GetColor(10.0));
            Assert.Equal(unevenColorScale.Colors[2], unevenColorScale.GetColor(15.0));

            Assert.Throws <ArgumentException>(() => unevenColorScale.GetColor(15.1));
        }
Пример #2
0
        public void DeserializesCorrectly()
        {
            var bandedColorScale = new ColorScale(new List <Color>()
            {
                Colors.Cyan, Colors.Purple, Colors.Orange
            }, 10);
            var bandedSerialized   = JsonConvert.SerializeObject(bandedColorScale);
            var bandedDeserialized = JsonConvert.DeserializeObject <ColorScale>(bandedSerialized);

            Assert.Equal(bandedColorScale.GetColor(0.12345), bandedDeserialized.GetColor(0.12345));

            var linearColorScale = new ColorScale(new List <Color>()
            {
                Colors.Cyan, Colors.Purple, Colors.Orange
            });
            var linearSerialized   = JsonConvert.SerializeObject(linearColorScale);
            var linearDeserialized = JsonConvert.DeserializeObject <ColorScale>(linearSerialized);

            Assert.Equal(linearColorScale.GetColor(0.12345), linearDeserialized.GetColor(0.12345));

            Assert.NotEqual(linearColorScale.GetColor(0.12345), bandedColorScale.GetColor(0.12345));
        }
Пример #3
0
        public void GetBandedColor()
        {
            var numColors  = 10;
            var colorScale = new ColorScale(new List <Color>()
            {
                Colors.Cyan, Colors.Purple, Colors.Orange
            }, numColors);

            Assert.Equal(10, colorScale.Colors.Count);
            for (var i = 0; i < numColors; i++)
            {
                Assert.Equal(colorScale.Colors[i], colorScale.GetColor((double)i / (numColors - 1)));
            }
        }
Пример #4
0
        private ImageSource DataToBitmap(float[] data, int2 dims)
        {
            ImageSource Bitmap;

            float Min   = (float)ThresholdLower;
            float Range = (float)(ThresholdUpper - ThresholdLower);

            if (Range == 0)
            {
                Range = 1e-5f;
            }

            ColorScale Palette = ColorScale == null ?
                                 new ColorScale(new[] { new float4(0, 0, 0, 1), new float4(1, 1, 1, 1) }) :
                                 ColorScale;

            byte[] DataBytes = new byte[data.Length * 4];
            for (int y = 0; y < dims.Y; y++)
            {
                for (int x = 0; x < dims.X; x++)
                {
                    float  V = (data[y * dims.X + x] - Min) / Range;
                    float4 C = Palette.GetColor(V) * 255;

                    DataBytes[((dims.Y - 1 - y) * dims.X + x) * 4 + 3] = (byte)C.W;
                    DataBytes[((dims.Y - 1 - y) * dims.X + x) * 4 + 2] = (byte)C.X;
                    DataBytes[((dims.Y - 1 - y) * dims.X + x) * 4 + 1] = (byte)C.Y;
                    DataBytes[((dims.Y - 1 - y) * dims.X + x) * 4 + 0] = (byte)C.Z;
                }
            }

            Bitmap = BitmapSource.Create(dims.X, dims.Y, 96, 96, PixelFormats.Bgra32, null, DataBytes, dims.X * 4);
            Bitmap.Freeze();

            return(Bitmap);
        }
        protected void PaintOnGraphicsVector(IGraphics g, int width, int height)
        {
            if (scatterPlot == null)
            {
                return;
            }
            if (!area.IsEmpty)
            {
                g.FillRectangle(Brushes.LightGray, 0, 0, width, height);
                g.FillRectangle(Brushes.White, area.X, area.Y, area.Width, area.Height);
            }
            else
            {
                g.FillRectangle(new SolidBrush(BackColor), 0, 0, width, height);
            }
            PaintGridVector(g, width, height);
            SymbolProperties[] gg = ArrayUtils.GetKeys(values);
            int[] counts          = new int[gg.Length];
            for (int i = 0; i < counts.Length; i++)
            {
                counts[i] = values[gg[i]].Count;
            }
            int[] o = ArrayUtils.Order(counts);
            Array.Reverse(o);
            gg = ArrayUtils.SubArray(gg, o);
            foreach (SymbolProperties g1 in gg)
            {
                bool[,] vals    = values[g1].Data;
                double[,] zvals = null;
                if (zValues != null)
                {
                    zvals = zValues[g1];
                }
                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < height; j++)
                    {
                        if (vals[i, j])
                        {
                            Color color;
                            if (zvals != null && !double.IsNaN(zvals[i, j]) && colorScale != null)
                            {
                                color = colorScale.GetColor(zvals[i, j]);
                            }
                            else
                            {
                                color = g1.Color;
                            }
                            Pen        p          = new Pen(color);
                            Brush      b          = new SolidBrush(color);
                            SymbolType symbolType = SymbolType.allSymbols[g1.Type];
                            int        symbolSize = g1.Size;
                            if (symbolSize == 0)
                            {
                                continue;
                            }
                            int[] pathX;
                            int[] pathY;
                            symbolType.GetPath(symbolSize, out pathX, out pathY);
                            symbolType.Draw(symbolSize, i, j, g, p, b);
                        }
                    }
                }
            }
            // selected data points
            Brush selectionBrush = new SolidBrush(SelectionColor);
            Pen   selectionPen   = new Pen(SelectionColor);

            foreach (int s in scatterPlot.Selection)
            {
                double[] w = scatterPlot.GetDataAt(s);
                if (w.Length > 0)
                {
                    double           x  = w[0];
                    double           y  = w[1];
                    SymbolProperties gx = GetPointProperties != null?GetPointProperties(s) : defaultSymbol;

                    SymbolType symbolType = SymbolType.allSymbols[gx.Type];
                    int        symbolSize = gx.Size;
                    if (symbolSize > 0)
                    {
                        symbolType.Draw(symbolSize, ModelToViewX(x, width), ModelToViewY(y, height), g, selectionPen, selectionBrush);
                    }
                }
            }
            // labels
            ScatterPlotLabelMode labelMode = scatterPlot.GetLabelMode();
            bool cutLabels = scatterPlot.CutLabels;

            if (labelMode == ScatterPlotLabelMode.All && scatterPlot.HasLabels)
            {
                Font font = new Font("Arial", scatterPlot.FontSize, scatterPlot.FontStyle);
                for (;;)
                {
                    double[] x;
                    double[] y;
                    double[] z;
                    string[] labels;
                    int      index;
                    scatterPlot.GetData(out x, out y, out z, out labels, out index);
                    if (x == null)
                    {
                        break;
                    }
                    SymbolProperties gx = GetPointProperties != null?GetPointProperties(index) : defaultSymbol;

                    for (int i = 0; i < x.Length; i++)
                    {
                        if (labelMode == ScatterPlotLabelMode.All || scatterPlot.IsSelected(i))
                        {
                            int   ix = ModelToViewX(x[i], width);
                            int   iy = ModelToViewY(y[i], height);
                            Color c;
                            if (z != null && colorScale != null)
                            {
                                c = colorScale.GetColor(z[i]);
                            }
                            else
                            {
                                c = gx.Color;
                            }
                            Brush b = new SolidBrush(c);
                            if (ix >= 0 && iy >= 0 && ix < width && iy < height)
                            {
                                if (labels[i] != null)
                                {
                                    string s = labels[i];
                                    if (cutLabels)
                                    {
                                        if (s.Contains(";"))
                                        {
                                            s = s.Substring(0, s.IndexOf(';'));
                                        }
                                    }
                                    g.DrawString(s, font, b, ix, iy);
                                }
                            }
                        }
                    }
                }
                scatterPlot.Reset();
            }
            if (labelMode == ScatterPlotLabelMode.Selected && scatterPlot.HasLabels)
            {
                Font font = new Font("Arial", scatterPlot.FontSize, scatterPlot.FontStyle);
                foreach (int s in scatterPlot.Selection)
                {
                    double[] w     = scatterPlot.GetDataAt(s);
                    string   label = scatterPlot.GetLabelAt(s);
                    double   x     = w[0];
                    double   y     = w[1];
                    int      ix    = ModelToViewX(x, width);
                    int      iy    = ModelToViewY(y, height);
                    if (ix >= 0 && iy >= 0 && ix < width && iy < height)
                    {
                        if (label != null)
                        {
                            if (cutLabels)
                            {
                                if (label.Contains(";"))
                                {
                                    label = label.Substring(0, label.IndexOf(';'));
                                }
                            }
                            g.DrawString(label, font, selectionBrush, ix, iy);
                        }
                    }
                }
            }
            DrawPolygons(g, width, height);
            if (drawFunctions != null)
            {
                drawFunctions(g, width, height, ModelToViewX, ModelToViewY, ViewToModelX, ViewToModelY);
            }
        }