SKShader Class

Definition

Shaders specify the source color(s) for what is being drawn in the SKPaint.

public class SKShader : SkiaSharp.SKObject
Inheritance

Examples

Linear Gradient Shader Example

var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
    SKCanvas canvas = surface.Canvas;

    canvas.Clear(SKColors.White);

    // create the shader
    var colors = new SKColor[] {
        new SKColor(0, 0, 255),
        new SKColor(0, 255, 0)
    };
    var shader = SKShader.CreateLinearGradient(
        new SKPoint(0, 0),
        new SKPoint(255, 255),
        colors,
        null,
        SKShaderTileMode.Clamp);

    // use the shader
    var paint = new SKPaint {
        Shader = shader
    };
    canvas.DrawPaint(paint);
}

The example above produces the following:

Linear Gradient

Radial Gradient Shader Example

var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
    SKCanvas canvas = surface.Canvas;

    canvas.Clear(SKColors.White);

    // create the shader
    var colors = new SKColor[] {
        new SKColor(0, 0, 255),
        new SKColor(0, 255, 0)
    };
    var shader = SKShader.CreateRadialGradient(
        new SKPoint(128, 128),
        180,
        colors,
        null,
        SKShaderTileMode.Clamp);

    // use the shader
    var paint = new SKPaint {
        Shader = shader
    };
    canvas.DrawPaint(paint);
}

The example above produces the following:

Radial Gradient

Two-point Conical Gradient Shader Example

var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
    SKCanvas canvas = surface.Canvas;

    canvas.Clear(SKColors.White);

    // create the shader
    var colors = new SKColor[] {
        new SKColor(0, 0, 255),
        new SKColor(0, 255, 0)
    };
    var shader = SKShader.CreateTwoPointConicalGradient(
        new SKPoint(128, 128),
        128,
        new SKPoint(128, 16),
        16,
        colors,
        null,
        SKShaderTileMode.Clamp);

    // use the shader
    var paint = new SKPaint {
        Shader = shader
    };
    canvas.DrawPaint(paint);
}

The example above produces the following:

Two-point Conical Gradient

Sweep Gradient Shader Example

var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
    SKCanvas canvas = surface.Canvas;

    canvas.Clear(SKColors.White);

    // create the shader
    var colors = new SKColor[] {
        new SKColor(0, 255, 255),
        new SKColor(255, 0, 255),
        new SKColor(255, 255, 0),
        new SKColor(0, 255, 255)
    };
    var shader = SKShader.CreateSweepGradient(
        new SKPoint(128, 128),
        colors,
        null);

    // use the shader
    var paint = new SKPaint {
        Shader = shader
    };
    canvas.DrawPaint(paint);
}

The example above produces the following:

Sweep Gradient

Fractal Perlin Noise Shader Example

var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
    SKCanvas canvas = surface.Canvas;

    canvas.Clear(SKColors.White);

    // create the shader
    var shader = SKShader.CreatePerlinNoiseFractalNoise(0.5f, 0.5f, 4, 0);

    // use the shader
    var paint = new SKPaint {
        Shader = shader
    };
    canvas.DrawPaint(paint);
}

The example above produces the following:

Fractal Perlin Noise

Perlin Noise Turbulence Shader Example

var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
    SKCanvas canvas = surface.Canvas;

    canvas.Clear(SKColors.White);

    // create the shader
    var shader = SKShader.CreatePerlinNoiseTurbulence(0.05f, 0.05f, 4, 0);

    // use the shader
    var paint = new SKPaint {
        Shader = shader
    };
    canvas.DrawPaint(paint);
}

The example above produces the following:

Fractal Perlin Noise

Compose Shader Example

var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
    SKCanvas canvas = surface.Canvas;

    canvas.Clear(SKColors.White);

    // create the first shader
    var colors = new SKColor[] {
        new SKColor(0, 255, 255),
        new SKColor(255, 0, 255),
        new SKColor(255, 255, 0),
        new SKColor(0, 255, 255)
    };
    var sweep = SKShader.CreateSweepGradient(new SKPoint(128, 128), colors, null);

    // create the second shader
    var turbulence = SKShader.CreatePerlinNoiseTurbulence(0.05f, 0.05f, 4, 0);

    // create the compose shader
    var shader = SKShader.CreateCompose(sweep, turbulence, SKBlendMode.SrcOver);

    // use the compose shader
    var paint = new SKPaint {
        Shader = shader
    };
    canvas.DrawPaint(paint);
}

The example above produces the following:

Compose Shader

Remarks

Shaders specify the source colors for what is being drawn. If a paint has no shader, then the paint's color is used. If the paint has a shader, then the shader's colors are used instead, but they are modulated by the paint's alpha.

This makes it easy to create a shader once (for example, bitmap tiling or gradient) and then change its transparency without having to modify the original shader, only the paint's alpha needs to be modified.

Shaders are created by calling one of the static "Create" methods.

Methods

CreateBitmap(SKBitmap, SKShaderTileMode, SKShaderTileMode)

Creates a new shader that will draw with the specified bitmap.

CreateBitmap(SKBitmap, SKShaderTileMode, SKShaderTileMode, SKMatrix)

Creates a new shader that will draw with the specified bitmap.

CreateColor(SKColor)

Creates a new shader that just draws the specified color.

CreateColorFilter(SKShader, SKColorFilter)

Creates a new shader that produces the same colors as invoking this shader and then applying the color filter.

CreateCompose(SKShader, SKShader)

Create a new compose shader, which combines two shaders by the SrcOver blend mode.

CreateCompose(SKShader, SKShader, SKBlendMode)

Create a new compose shader, which combines two shaders by a blend mode.

CreateCompose(SKShader, SKShader, SKXferMode)

Create a new compose shader, which combines two shaders by a transfer mode.

CreateEmpty()

Creates a new "empty" shader that will not draw anything.

CreateLinearGradient(SKPoint, SKPoint, SKColor[], Single[], SKShaderTileMode)

Creates a shader that generates a linear gradient between the two specified points.

CreateLinearGradient(SKPoint, SKPoint, SKColor[], Single[], SKShaderTileMode, SKMatrix)

Creates a shader that generates a linear gradient between the two specified points.

CreateLocalMatrix(SKShader, SKMatrix)

Creates a shader that first applies the specified matrix and then applies the shader.

CreatePerlinNoiseFractalNoise(Single, Single, Int32, Single)

Creates a new shader that draws Perlin fractal noise.

CreatePerlinNoiseFractalNoise(Single, Single, Int32, Single, SKPointI)

Creates a new shader that draws Perlin fractal noise.

CreatePerlinNoiseTurbulence(Single, Single, Int32, Single)

Creates a new shader that draws Perlin turbulence noise.

CreatePerlinNoiseTurbulence(Single, Single, Int32, Single, SKPointI)

Creates a new shader that draws Perlin turbulence noise.

CreateRadialGradient(SKPoint, Single, SKColor[], Single[], SKShaderTileMode)

Creates a shader that generates a radial gradient given the center and radius.

CreateRadialGradient(SKPoint, Single, SKColor[], Single[], SKShaderTileMode, SKMatrix)

Creates a shader that generates a radial gradient given the center and radius.

CreateSweepGradient(SKPoint, SKColor[], Single[])

Creates a shader that generates a sweep gradient given a center.

CreateSweepGradient(SKPoint, SKColor[], Single[], SKMatrix)

Creates a shader that generates a sweep gradient given a center.

CreateTwoPointConicalGradient(SKPoint, Single, SKPoint, Single, SKColor[], Single[], SKShaderTileMode)

Creates a shader that generates a conical gradient given two circles.

CreateTwoPointConicalGradient(SKPoint, Single, SKPoint, Single, SKColor[], Single[], SKShaderTileMode, SKMatrix)

Creates a shader that generates a conical gradient given two circles.

Dispose() Inherited from SKNativeObject
Dispose(Boolean)

Properties

Handle

Gets or sets the handle to the underlying native object.

(Inherited from SKObject)
OwnsHandle

Gets a value indicating whether this object should destroy the underlying native object.

(Inherited from SKObject)

Applies to