Example #1
0
        public static Bitmap Colourbar(Colourmap cmap, int width, int height, bool vertical = false)
        {
            if (width < 1 || height < 1)
            {
                return(null);
            }

            Bitmap bmp = new Bitmap(width, height);

            using (Graphics gfx = Graphics.FromImage(bmp))
                using (Pen pen = new Pen(Color.Magenta))
                {
                    if (vertical)
                    {
                        for (int y = 0; y < height; y++)
                        {
                            double fraction = (double)y / (height);
                            pen.Color = cmap.GetColour(fraction);
                            gfx.DrawLine(pen, 0, height - y - 1, width - 1, height - y - 1);
                        }
                    }
                    else
                    {
                        for (int x = 0; x < width; x++)
                        {
                            double fraction = (double)x / width;
                            pen.Color = cmap.GetColour(fraction);
                            gfx.DrawLine(pen, x, 0, x, height - 1);
                        }
                    }
                }
            return(bmp);
        }
Example #2
0
        public PlottableHeatmap(double[,] intensities, Colourmap colormap, string label, double[] axisOffsets, double[] axisMultipliers, double?scaleMin, double?scaleMax, double?transparencyThreshold, Bitmap backgroundImage, bool displayImageAbove, bool drawAxisLabels)
        {
            this.width  = intensities.GetLength(1);
            this.height = intensities.GetLength(0);
            double[] intensitiesFlattened = Flatten(intensities);
            this.min               = intensitiesFlattened.Min();
            this.max               = intensitiesFlattened.Max();
            this.brush             = new SolidBrush(Color.Black);
            this.pen               = new Pen(brush);
            this.axisOffsets       = axisOffsets;
            this.axisMultipliers   = axisMultipliers;
            this.colorMap          = colormap;
            this.label             = label;
            this.scaleMin          = scaleMin;
            this.scaleMax          = scaleMax;
            this.backgroundImage   = backgroundImage;
            this.displayImageAbove = displayImageAbove;
            this.drawAxisLabels    = drawAxisLabels;

            double normalizeMin = min;
            double normalizeMax = max;

            if (scaleMin.HasValue && scaleMin.Value < min)
            {
                normalizeMin = scaleMin.Value;
            }

            if (scaleMax.HasValue && scaleMax.Value > max)
            {
                normalizeMin = scaleMax.Value;
            }

            if (transparencyThreshold.HasValue)
            {
                this.transparencyThreshold = Normalize(new double[] { transparencyThreshold.Value }, min, max, scaleMin, scaleMax)[0];
            }

            intensitiesNormalized = Normalize(intensitiesFlattened, null, null, scaleMin, scaleMax);

            int[] flatARGB = Colourmap.GetRGBAs(intensitiesNormalized, colormap, minimumIntensity: transparencyThreshold ?? 0);

            bmp   = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            scale = new Bitmap(1, 256, PixelFormat.Format32bppArgb);

            double[] normalizedValues = Normalize(Enumerable.Range(0, scale.Height).Select(i => (double)i).Reverse().ToArray(), null, null, scaleMin, scaleMax);
            int[]    scaleRGBA        = Colourmap.GetRGBAs(normalizedValues, colormap);

            Rectangle  rect         = new Rectangle(0, 0, bmp.Width, bmp.Height);
            Rectangle  rectScale    = new Rectangle(0, 0, scale.Width, scale.Height);
            BitmapData bmpData      = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
            BitmapData scaleBmpData = scale.LockBits(rectScale, ImageLockMode.ReadWrite, scale.PixelFormat);

            Marshal.Copy(flatARGB, 0, bmpData.Scan0, flatARGB.Length);
            Marshal.Copy(scaleRGBA, 0, scaleBmpData.Scan0, scaleRGBA.Length);
            bmp.UnlockBits(bmpData);
            scale.UnlockBits(scaleBmpData);
        }
Example #3
0
 public static Color[] GetColours(double[] intensities, Colourmap colorMap)
 {
     Color[] colors = new Color[intensities.Length];
     for (int i = 0; i < intensities.Length; i++)
     {
         byte pixelIntensity = (byte)Math.Max(Math.Min(intensities[i] * 255, 255), 0);
         var(r, g, b) = colorMap.GetRGB(pixelIntensity);
         colors[i]    = Color.FromArgb(255, r, g, b);
     }
     return(colors);
 }
Example #4
0
        public PlottableVectorField(Vector2[,] vectors, double[] xs, double[] ys, string label, Color color, Colourmap colormap, double scaleFactor)
        {
            //the magnitude squared is faster to compute than the magnitude
            double minMagnitudeSquared = vectors[0, 0].Length();
            double maxMagnitudeSquared = vectors[0, 0].Length();

            for (int i = 0; i < xs.Length; i++)
            {
                for (int j = 0; j < ys.Length; j++)
                {
                    if (vectors[i, j].LengthSquared() > maxMagnitudeSquared)
                    {
                        maxMagnitudeSquared = vectors[i, j].LengthSquared();
                    }
                    else if (vectors[i, j].LengthSquared() < minMagnitudeSquared)
                    {
                        minMagnitudeSquared = vectors[i, j].LengthSquared();
                    }
                }
            }
            double minMagnitude = Math.Sqrt(minMagnitudeSquared);
            double maxMagnitude = Math.Sqrt(maxMagnitudeSquared);

            double[,] intensities = new double[xs.Length, ys.Length];

            for (int i = 0; i < xs.Length; i++)
            {
                for (int j = 0; j < ys.Length; j++)
                {
                    if (colormap != null)
                    {
                        intensities[i, j] = (vectors[i, j].Length() - minMagnitude) / (maxMagnitude - minMagnitude);
                    }
                    vectors[i, j] = Vector2.Multiply(vectors[i, j], (float)(scaleFactor / (maxMagnitude * 1.2))); //This is not a true normalize
                }
            }

            if (colormap != null)
            {
                double[] flattenedIntensities = intensities.Cast <double>().ToArray();
                arrowColors = Colourmap.GetColours(flattenedIntensities, colormap);
            }

            this.vectors     = vectors;
            this.xs          = xs;
            this.ys          = ys;
            this.label       = label;
            this.color       = color;
            this.colormap    = colormap;
            this.scaleFactor = scaleFactor;

            pen = new Pen(color);
            pen.CustomEndCap = new AdjustableArrowCap(2, 2);
        }
Example #5
0
 public static int[] GetRGBAs(double[] intensities, Colourmap colorMap, double minimumIntensity = 0)
 {
     int[] rgbas = new int[intensities.Length];
     for (int i = 0; i < intensities.Length; i++)
     {
         byte pixelIntensity = (byte)Math.Max(Math.Min(intensities[i] * 255, 255), 0);
         var(r, g, b) = colorMap.GetRGB(pixelIntensity);
         byte   alpha = pixelIntensity < minimumIntensity ? (byte)0 : (byte)255;
         byte[] argb  = { b, g, r, alpha };
         rgbas[i] = BitConverter.ToInt32(argb, 0);
     }
     return(rgbas);
 }