Build with XuiView Markdown

Colors, gradients, and compositing

Paint a rounded square with a purple-to-pink gradient, then place a translucent white circle over it. Tap to compare the translucent and opaque results.

The shape and the paint are separate decisions: the same path can use a solid color, a gradient, or an image pattern without changing its outline.

using System;
using Xui.Core.Canvas;
using Xui.Core.Math2D;
using Xui.Core.UI;
using Xui.Core.UI.Input;
using NFloat = System.Runtime.InteropServices.NFloat;

public sealed class PaintView : View
{
    private bool translucent = true;
    protected override void RenderCore(IContext context)
    {
        context.SetFill(new Color(0xFAF7FFFF));
        context.FillRect(Frame);
        NFloat side = NFloat.Max(0, NFloat.Min(Frame.Width - 48, 320));
        var area = new Rect(Frame.Center.X - side / 2, Frame.Center.Y - side / 2, side, side);
        context.Save();
        try
        {
            context.BeginPath();
            context.RoundRect(area, 28);
            context.Clip();
            ReadOnlySpan<GradientStop> stops = [new(0, 0x8538CEFF), new(1, 0xD957A8FF)];
            context.SetFill(new LinearGradient(area.TopLeft, area.BottomRight, stops));
            context.FillRect(area);
            context.GlobalAlpha = translucent ? (NFloat)0.35 : 1;
            context.SetFill(Colors.White);
            context.BeginPath();
            context.Arc(area.Center, side / 3, 0, NFloat.Pi * 2);
            context.Fill();
        }
        finally
        {
            context.Restore();
        }

        context.SetFill(new Color(0x492571FF));
        context.SetFont(new Font(18, "Inter"));
        context.TextAlign = TextAlign.Center;
        context.FillText("Tap to compare opacity", (Frame.Center.X, Frame.Y + 56));
    }

    public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase)
    {
        if (phase == EventPhase.Bubble && e.Type == PointerEventType.Down)
        {
            translucent = !translucent;
            InvalidateRender();
        }
    }
}
Gradients and opacity: initial rendered view
Gradients and opacity · C# source · SVG · Open demo

Read colors correctly

The packed Color constructor uses 0xRRGGBBAA: red, green, blue, then alpha. 0x8538CEFF is opaque purple. The final FF means full opacity; 00 means transparent. Do not copy an ARGB literal from another UI framework without reordering it.

Call SetFill for filled geometry and SetStroke for outlines. They keep separate paint state. Changing the fill does not recolor a previously drawn shape or change the next stroke.

Place the gradient in drawing coordinates

A LinearGradient has a start point, an end point, and ordered color stops. Its endpoints are coordinates, not percentages of the next shape. This example uses the square's top-left and bottom-right corners so the gradient always follows that square when the view resizes.

Stop offsets run from zero to one. Add a middle stop at 0.5 to introduce a third color. Keep the span alive through the SetFill call: LinearGradient is a ref struct that carries a span, not a long-lived brush object to store in a field. Store durable stop data separately if you reuse it across frames.

A radial gradient uses inner and outer circles instead of a line. Image patterns repeat sampled image content; they also require an image resource supported by the active backend.

Limit opacity and clipping to a scope

GlobalAlpha multiplies the opacity of subsequent drawing. It does not go back and change the gradient already painted. The white circle therefore reveals the gradient at 35 percent opacity and covers it when the value becomes one.

The example clips to a rounded rectangle before filling the square. A Save/Restore pair contains both the clip and the opacity change. The instruction label is drawn after restoration and remains fully opaque.

Do not treat per-operation alpha as group opacity: drawing two overlapping shapes at half opacity is not the same picture as composing an opaque group and fading the group once. Retained composition is a separate API with backend-specific behavior.

Stay within the available contract

The shared Canvas pen contract exposes colors, gradients, patterns, and alpha. It does not expose a general GlobalCompositeOperation or an arbitrary blend-mode selector. Do not translate a browser Canvas recipe by inventing that property. For an effect that depends on a different compositing operation, verify a supported composition/GPU path or render the effect into an image with an appropriate backend.

The software SVG and browser Canvas implementations provide the gradient, clip, and alpha operations used here. That does not establish parity for every filter or retained-surface effect.