Exemple #1
0
        /// <summary>
        /// Save this texture as a PNG.
        /// </summary>
        /// <param name="stream">Output stream.</param>
        public unsafe void SaveAsPng(Stream stream)
        {
            int w = Width;
            int h = Height;

            Color[] data = new Color[w * h];
            GetData(data);

            var bitmap   = new Bitmap(w, h);
            var bmp_data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

            for (var y = 0; y < bmp_data.Height; y++)
            {
                var rowPtr = (uint *)bmp_data.Scan0 + (y * bmp_data.Stride / 4);

                for (var x = 0; x < bmp_data.Width; x++)
                {
                    var c = data[x + ((h - y - 1) * w)];
                    var v = BitConverter.GetBytes(c.PackedValue);
                    rowPtr[x] = (uint)(v[0] + (v[1] << 8) + (v[2] << 16) + (v[3] << 24));
                }
            }

            bitmap.UnlockBits(bmp_data);
            bitmap.Save(stream, ImageFormat.Png);
        }
Exemple #2
0
        private void PickColor()
        {
            if (selectedEntry == null)
            {
                return;
            }

            var dlg = new ColorSelectionDialog("Pick a color");

            dlg.ColorSelection.HasOpacityControl = true;
            dlg.ColorSelection.CurrentColor      = new Gdk.Color(selectedEntry.Color.R, selectedEntry.Color.G, selectedEntry.Color.B);
            dlg.ColorSelection.CurrentAlpha      = ToGdkColor(selectedEntry.Color.A);
            dlg.Modal = true;

            dlg.Response += delegate(object o, ResponseArgs args)
            {
                if (args.ResponseId == ResponseType.Ok)
                {
                    var newColor = new Color(
                        FromGdkColor(dlg.ColorSelection.CurrentColor.Red),
                        FromGdkColor(dlg.ColorSelection.CurrentColor.Green),
                        FromGdkColor(dlg.ColorSelection.CurrentColor.Blue),
                        FromGdkColor(dlg.ColorSelection.CurrentAlpha));

                    selectedEntry = ReplaceEntry(selectedEntry, newColor);
                }
            };

            dlg.Run();
            dlg.Destroy();
        }
Exemple #3
0
        private GradientEntry ReplaceEntry(GradientEntry oldEntry, Color color)
        {
            var entry = new GradientEntry(oldEntry.Position, color);

            gradient.Remove(oldEntry);
            gradient.Add(entry);
            OnChange();
            return(entry);
        }
Exemple #4
0
        private Color GetComplementary2(Color color)
        {
            var c         = new Color(255 - color.B, 255 - color.G, 255 - color.R, 255);
            int darkest   = 50;
            int brightest = 255 + 255 + 255 - darkest;

            if (c.R + c.G + c.B < darkest)
            {
                return(Color.Red);
            }

            if (c.R + c.G + c.B > brightest)
            {
                return(Color.Red);
            }

            return(c);
        }
Exemple #5
0
        private void DrawBackground(Context ctx, RectangleF area)
        {
            var colorWhite = Color.White;
            var colorBlack = new Color(230, 230, 230, 255);
            int size       = 6;
            int count      = 0;

            for (int x = 0; x < area.Width / size; x++)
            {
                for (int y = 0; y < area.Height / size; y++)
                {
                    bool even = (x % 2 == 0) ^ (y % 2 == 0);

                    WidgetTools.SetColor(ctx, even ? colorWhite : colorBlack);
                    ctx.Rectangle(area.X + x * size, area.Y + y * size, size, size);
                    ctx.Fill();
                }
            }
        }
Exemple #6
0
        public void Draw(Drawable window, Context ctx, Rectangle bounds, StateType state)
        {
            if (gradient == null)
            {
                return;
            }

            int outerBoundsThickness = 4;

            var area                    = AreaFromBounds(new RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height));
            var outerBounds             = area.Inflated(outerBoundsThickness);
            var colorBorder             = new Color(204, 204, 204, 255);
            var colorSelectedBackground = new Color(154, 154, 154, 255);
            var colorEntry              = Color.Black;

            ctx.Save();

            ctx.Rectangle(outerBounds.X, outerBounds.Y, outerBounds.Width, outerBounds.Height);
            ctx.Clip();

            // Border
            WidgetTools.SetColor(ctx, state == StateType.Selected ? colorSelectedBackground : colorBorder);
            //SetColor(ctx, colorBorder);
            ctx.LineWidth = 1.0;
            ctx.Antialias = Antialias.Default;
            ctx.Rectangle(outerBounds.X, outerBounds.Y, outerBounds.Width, outerBounds.Height);
            ctx.Stroke();

            // Clip inner area
            ctx.Rectangle(outerBounds.X + 1, outerBounds.Y + 1, outerBounds.Width - 2, outerBounds.Height - 2);
            ctx.Clip();

            // Background
            DrawBackground(ctx, outerBounds);

            // Gradient
            if (gradient.Count > 0 && area.Width > 0)
            {
                int numSteps = (int)outerBounds.Width;

                // Could be done much cheaper with cairo gradients, but this way we'll show the actual values
                for (int i = 0; numSteps > 0 && i < numSteps; i++)
                {
                    var fraction = (i - outerBoundsThickness) / area.Width;
                    fraction = MathTools.ClampNormal(fraction);

                    WidgetTools.SetColor(ctx, gradient.Evaluate(fraction));

                    float x = outerBounds.Position.X + i;
                    float y = outerBounds.Position.Y;
                    ctx.MoveTo(x, y);
                    ctx.LineTo(x, y + outerBounds.Height);
                    ctx.Antialias = Antialias.None;
                    ctx.Stroke();
                }
            }

            // Entries
            foreach (var entry in gradient)
            {
                var slice = GetEntryArea(entry.Position, area);

                ctx.Antialias = Antialias.None;

                ctx.Rectangle(slice.X, slice.Y, slice.Width, slice.Height);
                WidgetTools.SetColor(ctx, entry.Color);
                ctx.Fill();

                Color edgeColor = entry == hoveredEntry?GetComplementary1(entry.Color) : GetComplementary2(entry.Color);

                ctx.Rectangle(slice.X, slice.Y, slice.Width, slice.Height);
                ctx.LineWidth = 1;
                WidgetTools.SetColor(ctx, edgeColor);
                ctx.Stroke();

                if (entry == selectedEntry)
                {
                    ctx.Rectangle(slice.X + 1, slice.Y + 1, slice.Width - 2, slice.Height - 2);
                    ctx.LineWidth = 1;
                    WidgetTools.SetColor(ctx, edgeColor);
                    ctx.Stroke();
                }
            }

            ctx.Restore();
        }