Category Archives: Composition API

Creating a fluid adaptive UI with VariableSizedWrapGrid and Implicit Animations

In this article we demonstrate an easy but powerful technique to implement a fluid adaptive UI for a XAML UWP app. The UI responds to changes in the page size by rearranging its elements. All elements are tilted and float smoothly to their new position. All you need to do, is select the correct main panel type, and call an extension method to hook up the animations.

ImplicitAnimation

What is a VariableSizedWrapGrid?

The VariableSizedWrapGrid is a layout panel that arranges it child elements in rows and columns, where each child element can span multiple rows and columns. These rows or columns automatically wrap to a new row or column. The Orientation property specifies the direction in which child elements are arranged and wrapped. The default size of an item or tile is determined by ItemHeight and ItemWidth, but individual items can demand more space through the ColumnSpan and RowSpan attached properties. When the control is resized (e.g. when the page is resized or rotated), the VariableSizedWrapGrid automatically rearranges its children. It seems that the VariableSizedWrapGrid has been mainly used for presenting collections of pictures and news items.

VariableSizedWrapGridSample

The VariableSizedWrapGrid is not an ItemsControl itself, but it can be used as ItemsPanel in a GridView to present item collections.

Hosting a UI Form in a VariableSizedWrapGrid

Its capabilities to host components of different sizes and to auto-(re)arrange their position make the VariableSizedWrapGrid a nice candidate panel for a responsive/adaptive UI. The control can be used as the main panel on your page. Specify at least values for ItemHeight, ItemWidth and Orientation. Also make sure that it can scroll in the opposite direction of its orientation. If you wrap horizontally then you should place it in a vertical ScrollViewer, like this:

<ScrollViewer VerticalScrollMode="Auto">
    <VariableSizedWrapGrid ItemHeight="100"
                           ItemWidth="250"
                           Orientation="Horizontal">
    <!-- UI Components here -->
    </VariableSizedWrapGrid>
</ScrollViewer>

You can now group all your input and other controls into appropriate subpanels (such as Grids and StackPanels), assign a value to the containers’ ColumnSpan and RowSpan and drop these into the VariableSizedWrapGrid:

<VariableSizedWrapGrid ItemHeight="100"
                       ItemWidth="250"
                       Orientation="Horizontal"
                       Margin="20 20 0 0">

    <Image VariableSizedWrapGrid.ColumnSpan="2"
            VariableSizedWrapGrid.RowSpan="3"
            ...
            Margin="0 0 20 20" />

    <TextBlock VariableSizedWrapGrid.ColumnSpan="2"
                VariableSizedWrapGrid.RowSpan="2"
                Padding="0 0 20 20"
                TextWrapping="WrapWholeWords">
    ... 
    </TextBlock>

    <StackPanel VariableSizedWrapGrid.ColumnSpan="1"
                VariableSizedWrapGrid.RowSpan="2"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Top"
                Padding="0 0 20 20">
        <!-- ... -->
    </StackPanel>

    <Grid VariableSizedWrapGrid.ColumnSpan="2"
            VariableSizedWrapGrid.RowSpan="2"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Padding="0 0 20 20">
        <!-- ... -->
    </Grid>

    <!-- ... -->
   
</VariableSizedWrapGrid>

To distribute horizontal and vertical spacing, I gave the host control a Margin of “20 20 0 0” and each subpanel a Margin or Padding of “0 0 20 20”.

When you resize the page, all subpanels will be automatically rearranged:
VariableSizedWrapGrid

VariableSizedWrapGrid_2

Alternatives

If you want more granular control over the layout of a page in different sizes, then you can switch to a design based on a RelativePanel and Adaptive Triggers. Please note that this also involves a lot more work for you.

It’s adaptive, now let’s make it fluid

The grid repositions its children when the page width changes. The transition is abrupt: all children are just smashed into their new location. Let’s smoothen this process and go from ‘Smash’ to ‘Whoosh’. We’ll animate the journey to the new position and add a gentle tilt effect while moving.

smash whoosh

The code we’ll be using is derived from the LayoutAnimation sample in the Windows UI Dev Labs repository on GitHub. We’re going to use implicit animations. For a deep dive into this topic, please read Exploring Implicit Animations, by Robert Mikhayelyan.

Implicit Animations start automatically after a trigger has been fired, so they help decouple animation from app logic. It’s the Composition Engine that does all of the work: it discovers when a trigger fires, and executes the animations. App developers semi-declaratively define the animations which they want to execute, and the events that trigger these animations. ‘Semi-declaratively’ in the previous sentence stands for ‘in C# with string-based expressions‘ (note: ‘declaratively’ would stand for ‘in XAML’).

In the documentation, at the bottom of each page dealing with one of the classes related to implicit animations, you see that they are relatively new to the framework:

  • Device family: Windows 10 Anniversary Edition (introduced v10.0.14393.0)
  • API contract: Windows.Foundation.UniversalApiContract (introduced v3)

This implies that we need to check the users’ SDK with a call to ApiInformation.IsApiContractPresent before we can use these classes from our code:

// Check if SDK > 14393
if (!ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 3))
{
    return;
}

An ImplicitAnimationCollection can be defined on the following properties on Visual:

  • AnchorPoint
  • CenterPoint
  • Offset
  • Opacity
  • Orientation
  • RotationAngle
  • RotationAngleInDegrees
  • RotationAxis
  • Scale
  • Size

The Offset property is the one we’re interested in. It corresponds to the relative position of a Visual (every subpanel) in its container (the VariableSizedWrapGrid).

The API and its documentation feel a bit swampy here, but you should

Here’s the code. It’s written as an extension method of Panel. So it applies not only to VariableSizedWrapView but also to Canvas, Grid and StackPanel:

public static void RegisterImplicitAnimations(this Panel panel)
{
    // Check if SDK > 14393
    if (!ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 3))
    {
        return;
    }

    var compositor = ElementCompositionPreview.GetElementVisual(panel).Compositor;

    // Create ImplicitAnimations Collection. 
    var elementImplicitAnimation = compositor.CreateImplicitAnimationCollection();

    // Define trigger and animation that should play when the trigger is triggered. 
    elementImplicitAnimation["Offset"] = CreateOffsetAnimation(compositor);

    foreach (var item in panel.Children)
    {
        var elementVisual = ElementCompositionPreview.GetElementVisual(item);
        elementVisual.ImplicitAnimations = elementImplicitAnimation;
    }
}

Here’s the code for the individual animation for each element. It’s a combination of two CompositionAnimation instances, each with a Duration of 0.4 seconds:

The code is a copy/paste from the LayoutAnimations sample. I didn’t feel the need to change any of the parameters. After all, this was done by designers:

private static CompositionAnimationGroup CreateOffsetAnimation(Compositor compositor)
{
    // Define Offset Animation for the Animation group
    var offsetAnimation = compositor.CreateVector3KeyFrameAnimation();
    offsetAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
    offsetAnimation.Duration = TimeSpan.FromSeconds(.4);

    // Define Animation Target for this animation to animate using definition. 
    offsetAnimation.Target = "Offset";

    // Define Rotation Animation for Animation Group. 
    var rotationAnimation = compositor.CreateScalarKeyFrameAnimation();
    rotationAnimation.InsertKeyFrame(.5f, 0.160f);
    rotationAnimation.InsertKeyFrame(1f, 0f);
    rotationAnimation.Duration = TimeSpan.FromSeconds(.4);

    // Define Animation Target for this animation to animate using definition. 
    rotationAnimation.Target = "RotationAngle";

    // Add Animations to Animation group. 
    var animationGroup = compositor.CreateAnimationGroup();
    animationGroup.Add(offsetAnimation);
    animationGroup.Add(rotationAnimation);

    return animationGroup;
}

Thanks to the extension method, we can go from ‘Smash’ to ‘Whoosh’ with just one line of code:

// Yep: that's all.
VariableSizedWrapGrid.RegisterImplicitAnimations();

Here are some action shots from the animation:
ImplicitAnimation

ImplicitAnimation_2

Source Code

The code lives here on GitHub.

Enjoy!

A splash screen animation for UWP

What’s a splash screen if it doesn’t splash?

This article shows how to zoom the splash screen of a UWP app to the size of its window with an animation that doesn’t use the UI thread. This is how it looks like in a sample app:

AnimatedSplashScreen

The splash screen grows and gets transparent while the app is navigating to its main page. I did not wrote the feature myself, the beef of the code can be found in the Windows UI Dev Labs. I just refactored the code, made it more generic, and packaged it for easy reuse. I was able to implement it as an extension method of the Page class.

All you need to do to add the animation to your app is adding one line of code in the Application.OnLaunched method (in app.xaml.cs):

if (rootFrame.Content == null)
{
    rootFrame.Navigate(typeof(Shell), e.Arguments);
    (rootFrame.Content as Page).OpenFromSplashScreen(
	e.SplashScreen.ImageLocation);
}

// Ensure the current window is active
Window.Current.Activate();

While the app navigates to its first page, a copy of the splash screen (or another image if you want) is placed on the position of the original image. The LaunchActivatedEventArgs instance from OnLaunched has a reference to the SplashScreen instance, which exposes its ImageLocation. That rectangle is passed to the OpenFromSplashScreen call, optionally together with the path to the image file, and a background color (in case you have a transparent image).

Here’s an alternative call, we provide the path to the (transparent) splash screen of the UWP Community Toolkit, and specify a black background:

(rootFrame.Content as Page).OpenFromSplashScreen(
	e.SplashScreen.ImageLocation, 
	Colors.Black, 
	new Uri("ms-appx:///Assets/ToolkitLogoTransparent.png"));

The OpenFromSplashScreen method first registers a handler to the Loaded event, to start the animation. Then the SurfaceLoader helper instance is initialized, and finally we display a copy of the splash screen image that will be animated later:

public static void OpenFromSplashScreen(
	this Page page, 
	Rect imageBounds, 
	Color backgroundColor, 
	Uri imageUri)
{
    page.Loaded += Page_Loaded;

    // Initialize the surface loader
    SurfaceLoader.Initialize(
	ElementCompositionPreview.GetElementVisual(page).Compositor);

    // Show the custom splash screen
    ShowImage(page, imageBounds, imageUri, backgroundColor);
}

The copy of the splash screen image is entirely drawn through the Composition API. Via ElementCompositionPreview.GetElementVisual we get access to the page’s Visual and its Compositor. This compositor factory is used to create a ContainerVisual that will have the background color (a SpriteVisual painted with a solid CompositionColorBrush) and the image (in a CompositionDrawingSurface) as its Children:

private static async void ShowImage(Page page, Rect imageBounds, Uri imageUri, Color backgroundColor)
{
    var compositor = ElementCompositionPreview.GetElementVisual(page).Compositor;
    var windowSize = new Vector2((float)Window.Current.Bounds.Width, (float)Window.Current.Bounds.Height);

    //
    // Create a container visual to hold the color fill background and image visuals.
    // Configure this visual to scale from the center.
    //
    var container = compositor.CreateContainerVisual();
    container.Size = windowSize;
    container.CenterPoint = new Vector3(windowSize.X, windowSize.Y, 0) * .5f;
    ElementCompositionPreview.SetElementChildVisual(page, container);

    //
    // Create the colorfill sprite for the background, set the color to the same as app theme
    //
    var backgroundSprite = compositor.CreateSpriteVisual();
    backgroundSprite.Size = windowSize;
    backgroundSprite.Brush = compositor.CreateColorBrush(backgroundColor);
    container.Children.InsertAtBottom(backgroundSprite);

    //
    // Create the image sprite containing the splash screen image.  Size and position this to
    // exactly cover the Splash screen image so it will be a seamless transition between the two
    //
    var surface = await SurfaceLoader.LoadFromUri(imageUri);
    var imageSprite = compositor.CreateSpriteVisual();
    imageSprite.Brush = compositor.CreateSurfaceBrush(surface);
    imageSprite.Offset = new Vector3((float)imageBounds.X, (float)imageBounds.Y, 0f);
    imageSprite.Size = new Vector2((float)imageBounds.Width, (float)imageBounds.Height);
    container.Children.InsertAtTop(imageSprite);
}

When the main page is loaded (in the sample app, it’s the shell that hosts the main SplitView), the animation is started:

private static void Page_Loaded(object sender, RoutedEventArgs e)
{
    (sender as Page).Loaded -= Page_Loaded;

    // Now that loading is complete, dismiss the custom splash screen
    ShowContent(sender as Page);
}

The animation contains a ScalarKeyFrameAnimation for the opacity, and a Vector2KeyFrameAnimation for the image’s size. Both animations are started through a StartAnimation call, right after the declaration of a ScopedBatch instance on the same compositor:

private static void ShowContent(Page page)
{
    var container = (ContainerVisual)ElementCompositionPreview.GetElementChildVisual(page);
    var compositor = container.Compositor;

    // Setup some constants for scaling and animating
    const float scaleFactor = 7.5f;
    var duration = TimeSpan.FromMilliseconds(2000);

    // Create the fade animation which will target the opacity of the outgoing splash screen
    var fadeOutAnimation = compositor.CreateScalarKeyFrameAnimation();
    fadeOutAnimation.InsertKeyFrame(1, 0);
    fadeOutAnimation.Duration = duration;

    // Create the scale up animation for the Splash screen visuals
    var scaleUpSplashAnimation = compositor.CreateVector2KeyFrameAnimation();
    scaleUpSplashAnimation.InsertKeyFrame(0, new Vector2(1, 1));
    scaleUpSplashAnimation.InsertKeyFrame(1, new Vector2(scaleFactor, scaleFactor));
    scaleUpSplashAnimation.Duration = duration;

    // Configure the visual to scale from the center
    var frameworkElement = page.Content as FrameworkElement;
    var visual = ElementCompositionPreview.GetElementVisual(frameworkElement);
    visual.Size = new Vector2((float)frameworkElement.ActualWidth, (float)frameworkElement.ActualHeight);
    visual.CenterPoint = new Vector3(visual.Size.X, visual.Size.Y, 0) * .5f;

    //
    // Create a scoped batch for the animations.  When the batch completes, we can dispose of the
    // splash screen visuals which will no longer be visible.
    //
    var batch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);

    container.StartAnimation("Opacity", fadeOutAnimation);
    container.StartAnimation("Scale.XY", scaleUpSplashAnimation);

    currentPage = page; // TODO: find a better way to pass the page to the event.
    batch.Completed += Batch_Completed;
    batch.End();
}

This enables us to clean up all Composition API instances in the batch’s Completed event:

private static void Batch_Completed(
	object sender, 
	CompositionBatchCompletedEventArgs args)
{
    // Now that the animations are complete, dispose of the custom Splash Screen visuals
    ElementCompositionPreview.SetElementChildVisual(currentPage, null);
}

What about the phone?

The animated splash screen is a UWP library, so it also works on your Windows phone. But on such a small-screen device the splash screen is relatively big (its width is the same as the whole screen width). So the animation is less noticeable than on a PC. For smaller screens it makes sense to let the whole content zoom in, instead of just the splash screen. The code for that is in the code base, but commented out.

Where’s the code?

The library and the sample app are hosted on GitHub. if you want to add the effect right away to your app then just install its NuGet package.

Enjoy!

BackDrop: a control for dynamic blur in UWP

This article presents a lightweight UWP control that creates a configurable blur and tint effect and applies it on the pixels behind it. It’s intended to be used as background of panels, flyouts and dialogs. Here’s how the control looks like in action – it’s the semitransparent red panel in the middle of the page:

Properties

I did not create the control myself, all credits should go the team that built the Composition API Samples on GitHub. When you open the menu of the sample gallery app, you immediately notice the SplitView Pane’s background. Mainly because it is freshly green, but also because it blurs whatever’s underneath it:

SplitViewMenu

When diving into the source code, I discovered the BackDrop control and immediately decided to give it a spin.

The BackDrop control is XAML-wise a very lightweight control: it derives from Control and has no UI elements of its own – so there’s no style or template for it.

public class BackDrop : Control
{
	// ...
}

The BackDrop control uses the Composition API. The dynamic blur effect comes from a CompositionBrush that is applied to a SpriteVisual. This visual is hooked in the control’s (empty) XAML tree with SetElementChildVisual. While a ‘regular’ brush applies to the pixels of its host, this one is defined using CreateBackDropBrush. So it applies to all pixels underneath the surface of its host control:

public BackDrop()
{
    var rootVisual = ElementCompositionPreview.GetElementVisual(this);
    _compositor = rootVisual.Compositor;
    _blurVisual = _compositor.CreateSpriteVisual();

    var brush = BuildBlurBrush();
    brush.SetSourceParameter("Source", _compositor.CreateBackdropBrush());
    _blurBrush = brush;
    _blurVisual.Brush = _blurBrush;

    ElementCompositionPreview.SetElementChildVisual(this, _blurVisual);
}

The main brush is a CompositionEffectBrush that combines not two but three effects. There’s obviously a GaussianBlur and a ColorSource effect involved. On top of these there’s a Saturation effect that enhances the background color by moving the colors away from grey. Here’s a screenshot from panels with a BackDrop with (on the right) and without (on the left) the saturation effect:

SaturationEffect

[If you prefer the panel on the left: the saturation intensity is configurable, and a value of ‘1’ means ‘no effect’.]

I modified the original code of the BackDrop control to expose all relevant parameters of its brush as dependency properties, so you can two-way-bind to these through XAML and C#:

BlurAmount double Gets or sets the amount of blur to apply on the background.
TintColor Color Gets or sets the color for the ColorSource effect. The Alpha value will be ignored.
TintAlpha int Gets or sets the transparency of the TintColor.
SaturationIntensity double Gets or sets the saturation. 0 is monochrome.

Here’s how the main brush is built up, with BlurAmount, Color, and Saturation as so-called animatable properties:

private CompositionEffectBrush BuildBlurBrush()
{
    var blurEffect = new GaussianBlurEffect()
    {
        Name = "Blur",
        BlurAmount = 0.0f,
        BorderMode = EffectBorderMode.Hard,
        Optimization = EffectOptimization.Balanced,
        Source = new CompositionEffectSourceParameter("Source")
    };

    var blendEffect = new BlendEffect
    {
        Background = blurEffect,
        Foreground = new ColorSourceEffect()
        {
            Name = "Color",
            Color = Color.FromArgb(90, 255, 255, 255)
        },
        Mode = BlendEffectMode.SoftLight
    };

    var saturationEffect = new SaturationEffect
    {
        Name = "Saturation",
        Source = blendEffect,
        Saturation = 1.75f
    };

    var factory = _compositor.CreateEffectFactory(
        saturationEffect,
        new[] { "Blur.BlurAmount", "Color.Color", "Saturation.Saturation" });

    return factory.CreateBrush();
}

Here are the PropertyChangedCallback delegates that update the effect parameters:

private static void OnBlurAmountChanged(
	DependencyObject d, 
	DependencyPropertyChangedEventArgs e)
{
    var backDrop = d as BackDrop;

    if (backDrop == null) return;

    backDrop._blurBrush.Properties.InsertScalar(
	"Blur.BlurAmount", 
	(float)(double)e.NewValue);
}

private static void OnTintColorChanged(
	DependencyObject d, 
	DependencyPropertyChangedEventArgs e)
{
    var backDrop = d as BackDrop;

    if (backDrop == null) return;

    var color = (Color)e.NewValue;
    color.A = (byte)backDrop.TintAlpha;

    backDrop._blurBrush.Properties.InsertColor("Color.Color", color);
}

private static void OnTintAlphaChanged(
	DependencyObject d, 
	DependencyPropertyChangedEventArgs e)
{
    var backDrop = d as BackDrop;

    if (backDrop == null) return;

    var color = backDrop.TintColor;
    color.A = (byte)(int)e.NewValue;

    backDrop._blurBrush.Properties.InsertColor("Color.Color", color);
}

The home page of my sample app allows you to play with all of the parameters. While experimenting, don’t forget to resize the app to observe the dynamic blur effect:

Properties

The main purpose of the BackDrop control is to be used as background for different panels, e.g. a Flyout. Here’s a screenshot of the Flyout page of the sample app. This page demonstrates the impact of BackDrop control. It shows the same content in two panels: the panel on the left has a BackDrop in its XAML, the panel on the right doesn’t.

Press the button to open a real Flyout and observe a cumulated blur and tint effect:

FlyOut

Here’s part of the XAML for the fixed panel on the left. To use a BackDrop, just put the control inside a transparent container:

<Grid Background="Transparent"">
    <controls:BackDrop BlurAmount="25"
                        TintColor="BlanchedAlmond" />
    <Grid>
        <StackPanel Padding="10">
        <!-- ... -->
        </StackPanel>
    </Grid>
</Grid>

Here’s how to hook a BackDrop control in a real Flyout. Don’t forget the Style Setter that makes the background transparent:

<Flyout>
    <Flyout.FlyoutPresenterStyle>
        <Style TargetType="FlyoutPresenter">
            <Setter Property="Background"
                    Value="Transparent" />
            <Setter Property="Padding"
                    Value="0" />
        </Style>
    </Flyout.FlyoutPresenterStyle>
    <Grid Background="Transparent"
            Width="300">
        <controls:BackDrop BlurAmount="25"
                            TintColor="BlanchedAlmond" />
        <Grid>
        <!-- ... -->
        </Grid>
    </Grid>
</Flyout>

While it’s easy to use the BackDrop control as a background for panels and Flyouts, using it inside a ContentDialog is a bit more challenging. The ContentDialog control is protected, in the sense that you can’t create a template for it in Visual Studio or Blend. You’re only supposed to modify the TitleTemplate and/or the ContentTemplate:

ContentDialogTemplate

To restyle an entire ContentDialog control, you can copy its style from the main Generic.xaml (which you find in \(Program Files)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\<SDK version>\Generic) and use a modified version of it as a XAML resource:

<Page.Resources>
    <!-- Custom style for Windows.UI.Xaml.Controls.ContentDialog -->
    <!-- Gives it a transparent background and adds a BackDrop -->
    <Style TargetType="ContentDialog">
        <Setter Property="Foreground"
                Value="{ThemeResource SystemControlPageTextBaseHighBrush}" />
        <Setter Property="Background"
                Value="Transparent" />
        <!-- ... -->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentDialog">
                    <Border x:Name="Container">
                        <Grid x:Name="LayoutRoot">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <controls:BackDrop BlurAmount="15"
                                                TintColor="BurlyWood" />
                            <Border x:Name="BackgroundElement"
                                    Background="{TemplateBinding Background}"
                                    ...
                                    MinHeight="{TemplateBinding MinHeight}">
                                <Grid x:Name="DialogSpace"
                                        VerticalAlignment="Stretch"
                                        Padding="0 20">
                                <!-- ... -->
                                </Grid>
                            </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

This is the result – a modal dialog box with a blurred and tinted background:

Dialog

Of course I wanted to stress test the BackDrop control in my favorite control torture chamber: the SquareOfSquares. Here’s how a group of 21 differently configured BackDrop controls look like on a single page:

Squares

Here’s the same page on my phone:

BackDrop_Phone

When you look at the Memory Usage Diagnostic Tool in Visual Studio, you’ll notice that this page indeed consumes some extra memory, but it’s not a really significant amount of megabytes and most of it (not all!) is rapidly released. Anyway, this single page has more BackDrop controls than you’ll ever need in an app, so I assume it’s pretty safe to use it.

I didn’t feel the need to animate the effect. If you want to add your own animation (e.g. in the Loaded event), just take a look at the source page of this sample page from the Composition API Sample app:

BackDropSample

My version of the BackDrop control and its sample app live here on GitHub. The control is in its own library, for easy reuse. Make sure to have Anniversary Update or higher, and reference the Win2D UWP NuGet package.

Enjoy!

Building a UWP Rating Control using XAML and the Composition API

 

In this article we’ll build a XAML and C# Rating Control for the Windows Universal Platform which will be entirely drawn by the Composition API. It’s a custom Control that comes with the following dependency properties:

  • Maximum (int): the number of stars (or other images) to display,
  • StepFrequency (double): the rounding interval for the Value (a percentage, e.g. 0.25)
  • Value (double): the current value (from 0 to Maximum)
  • ItemHeight (int): height (and width) of each image in device independent pixels
  • ItemPadding (int): the number of pixels between images
  • FilledImage (uri): path to the filled image
  • EmptyImage (uri): path to the empty image
  • IsInteractive (bool): whether or not the control responds to user input (tapping or sliding)

The names of the core properties (Maximum, StepFrequency, and Value) are borrowed from the Slider class because after all –just like the slider- a Rating control is just a control to set and display a value within a range.

The Rating control’s behavior is also inspired by the slider:

  • tap on an image to set a value, and
  • slide horizontally over the control to decrease and increase the value with StepFrequency steps.

Here are some instances of the control in action:

WithoutLossOfImageQuality

An almost empty XAML template

The UI of the control is drawn entirely using the Composition API, so I kept the XAML template as simple as possible. I was tempted to use an ItemsControl as basis, but went for a Panel.. If the control were purely XAML, then a horizontal StackPanel would suffice as ControlTemplate. The star (or other) images will be displayed using the Composition API, in a layer on top of that StackPanel. This layer makes the panel itself unable to detect Tapped or ManipulationDelta events. The template contains extra Grid controls to put a ‘lid’ on the control.

The control template makes the distinction between the part that displays the images (PART_Images), and the part that deals with user input (PART_Interaction) through touch, pen, mouse or something else (like X-Box controller or Kinect – remember it’s a UWP app).

Here’s the default style definition in Themes/Generic.xaml:

<Style TargetType="local:Rating">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:Rating">
                <Grid>
                    <!-- Holds the images. -->
                    <StackPanel x:Name="PART_Items"
                                Orientation="Horizontal"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center" />
                    <!-- Interacts with touch and mouse and so. -->
                    <Grid x:Name="PART_Interaction"
                            ManipulationMode="TranslateX"
                            Background="Transparent" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The code behind

Set-up

By convention, all named elements in the style start their name with “PART_” and are decorated with a TemplatePart:

[TemplatePart(Name = ItemsPartName, Type = typeof(StackPanel))]
[TemplatePart(Name = InteractionPartName, Type = typeof(UIElement))]
public class Rating : Control
{
    private const string ItemsPartName = "PART_Items";
    private const string InteractionPartName = "PART_Interaction";

    // ...
}

All properties are defined as Dependency Property, which allows two-way binding in XAML and automatic property change notification. All of the properties also have a default value, so that the control can be used immediately without specifying mandatory property values. And last but not least, all of the properties have a PropertyChanged callback in their PropertyMetadata, so the UI of the control is updated automatically at runtime when one of the properties changes. The dependency property registrations use the nameof() expression instead of a hard-coded string [which is still in the propdp code snippet].

Here’s how the ItemHeight property is registered:

public static readonly DependencyProperty ItemHeightProperty = 
DependencyProperty.Register(
    nameof(ItemHeight),
    typeof(int),
    typeof(Rating),
    new PropertyMetadata(12, OnStructureChanged));

In the OnApplyTemplate the control is drawn by a call to OnStructureChanged – the property changed callback that also redraws the control at runtime – and the event handlers for touch interaction –Tapped and ManipulationDelta– are registered:

protected override void OnApplyTemplate()
{
    // Ensures that ActualWidth is actually the actual width.
    HorizontalAlignment = HorizontalAlignment.Left;

    OnStructureChanged(this);

    var surface = this.GetTemplateChild(InteractionPartName) as UIElement;
    if (surface != null)
    {
        surface.Tapped += Surface_Tapped;
        surface.ManipulationDelta += Surface_ManipulationDelta;
    }

    base.OnApplyTemplate();
}

The OnstructureChanged method is called when the control is rendered initially, or whenever one of the main UI characteristics is changed (things like item height or padding, maximum, ore one of the images).

The method starts with verifying if the user provided custom images. If not, a default empty and full star image is taken from the control’s own resources. To my surprise, the initialization of the default image path did not work in the dependency property registration, nor in OnApplyTemplate:

private static void OnStructureChanged(DependencyObject d)
{
    Rating c = (Rating)d;

    if (c.EmptyImage == null)
    {
        c.EmptyImage = new Uri(
	"ms-appx:///XamlBrewer.Uwp.RatingControl/Assets/defaultStar_empty.png");
    }

    if (c.FilledImage == null)
    {
        c.FilledImage = new Uri(
	"ms-appx:///XamlBrewer.Uwp.RatingControl/Assets/defaultStar_full.png");
    }

    // ...
}

The next step in OnStructureChanged is to make sure that the StepFrequency falls in the expected range, which is greater than zero but maximum one:

if ((c.StepFrequency <= 0) || (c.StepFrequency > 1))
{
    c.StepFrequency = 1;
}

Loading the images

Then it’s time to load the two images. In the current version of the Composition API you’ll need some extra help for this. My favorite helper is the Microsoft.UI.Composition.Toolkit, a small C++ project that comes with the Windows UI Dev Labs samples on GitHub:

MSUICompositionToolkit

Every image is loaded once into a CompositionSurfaceBrush that we’ll reuse for each item in the list of rating images. Here’s the code that creates the two brushes:

var panel = c.GetTemplateChild(ItemsPartName) as StackPanel;
if (panel != null)
{
    // ...

    // Load images.
    var root = panel.GetVisual();
    var compositor = root.Compositor;
    var options = new CompositionImageOptions()
    {
        DecodeWidth = c.ItemHeight,
        DecodeHeight = c.ItemHeight
    };
    var imageFactory = 
	CompositionImageFactory.CreateCompositionImageFactory(compositor);
    var image = imageFactory.CreateImageFromUri(c.EmptyImage, options);
    var emptyBrush = compositor.CreateSurfaceBrush(image.Surface);
    image = imageFactory.CreateImageFromUri(c.FilledImage, options);
    var fullBrush = compositor.CreateSurfaceBrush(image.Surface);

    // ...
}

The reason why I prefer to use the Composition Toolkit for loading images is the fact that you can control the DecodeWidth and DecodeHeight. Alternatively, you can use the C# CompositionImageLoader project, also on GitHub. It comes with a NuGet package:

CompositionImageLoader

Here’s how the code looks like when you use this library:

// Load images.
var root = panel.GetVisual();
var compositor = root.Compositor;
var imageLoader = ImageLoaderFactory.CreateImageLoader(compositor);
var surface = imageLoader.LoadImageFromUri(c.EmptyImage);
var emptyBrush = compositor.CreateSurfaceBrush(surface);
surface = imageLoader.LoadImageFromUri(c.FilledImage);
var fullBrush = compositor.CreateSurfaceBrush(surface);

I had the intention to copy relevant code of the CompositionImageLoader into my project in order to create a full C# control with as few as possible external dependencies (only Win2D). But then I noticed a loss in image quality when using CompositionImageLoader. It looks like there’s a loss in DPI, even if you specify the size of the target image on load:

surface = imageLoader.LoadImageFromUri(
	c.FilledImage, 
	new Size(c.ItemHeight, c.ItemHeight));

Here’s a screenshot of the sample app using CompositionImageLoader:

LossOfImageQuality

And here’s the same app using Micsosoft.Composition.UI.Toolkit:

WithoutLossOfImageQuality

There’s a significant loss of quality in the devil and 3D star images. To see it, you may need to click on the screenshots to see them in full size, or try another monitor – the difference is not always obvious. Anyway, it made me hit the undo button in Source Control…

Rendering the control

The two composition surface brushes are loaded into SpriteVisual instances that are hooked to a padded Grid that is created for each item in the list of rating images. The full image will be drawn on top of the empty one. Based on the Value, we’ll calculate the clipping rectangle for each ‘full’ image. Here’s a 3D view on the structure. The yellow surface represents the StackPanel from the control’s template, the green rectangles are the root Grid elements for each image, and the images are … well … the images:

RatingStructure

At runtime, we’ll change the InsetClip values of the images on top, so the control maintains the references to these:

private List<InsetClip> Clips { get; set; } = new List<InsetClip>();

Here’s the code that creates all the layers – the full images are right-clipped at zero, so they don’t appear:

var rightPadding = c.ItemPadding;
c.Clips.Clear();

for (int i = 0; i < c.Maximum; i++)
{
    if (i == c.Maximum - 1)
    {
        rightPadding = 0;
    }

    // Create grid.
    var grid = new Grid
    {
        Height = c.ItemHeight,
        Width = c.ItemHeight,
        Margin = new Thickness(0, 0, rightPadding, 0)
    };
    panel.Children.Add(grid);
    var gridRoot = grid.GetVisual();

    // Empty image.
    var spriteVisual = compositor.CreateSpriteVisual();
    spriteVisual.Size = new Vector2(c.ItemHeight, c.ItemHeight);
    gridRoot.Children.InsertAtTop(spriteVisual);
    spriteVisual.Brush = emptyBrush;

    // Filled image.
    spriteVisual = compositor.CreateSpriteVisual();
    spriteVisual.Size = new Vector2(c.ItemHeight, c.ItemHeight);
    var clip = compositor.CreateInsetClip();
    c.Clips.Add(clip);
    spriteVisual.Clip = clip;
    gridRoot.Children.InsertAtTop(spriteVisual);
    spriteVisual.Brush = fullBrush;
}

We’re at the end of the OnstructureChanged code now. The control is rendered or re-rendered with the correct number of the correct images at the correct size and padding. It’s time to update the value:

OnValueChanged(c);

Changing the value

When the Value of the control is changed, we calculate the InsetClip for each image in the top layer (the ‘full’ stars). The images left of the value will be fully shown (clipped to the full width), the images right of the value will be hidden (clipped to zero). For the image in the middle, we calculate the number of pixels to be shown:

private static void OnValueChanged(DependencyObject d)
{
    Rating c = (Rating)d;

    var panel = c.GetTemplateChild(ItemsPartName) as StackPanel;
    if (panel != null)
    {
        for (int i = 0; i < c.Maximum; i++)
        {
            if (i <= Math.Floor(c.Value - 1))
            {
                // Filled image.
                c.Clips[i].RightInset = 0;
            }
            else if (i > Math.Ceiling(c.Value - 1))
            {
                // Empty image.
                c.Clips[i].RightInset = c.ItemHeight;
            }
            else
            {
                // Curtain.
                c.Clips[i].RightInset = 
	(float)(c.ItemHeight * (1 +  Math.Floor(c.Value) - c.Value));
            }
        }
    }
}

The images come from reusable brushes and are never reloaded at runtime, so I think that this rating control is very efficient in its resource usage.

The behavior

The Value property changes by sliding over the image. We have to round it to the nearestStepFrequency fraction. Here’s the rounding routine:

public static double RoundToFraction(double number, double fraction)
{
    // We assume that fraction is a value between 0 and 1.
    if (fraction <= 0) { return 0; }
    if (fraction > 1) { return number; }

    double modulo = number % fraction;
    if ((fraction - modulo) <= modulo)
        modulo = (fraction - modulo);
    else
        modulo *= -1;

    return number + modulo;
}

The behavior of the rating control is defined by two interactions:

  • tapping for fast initialization, and
  • sliding to adjust more precisely.

As already mentioned, the event handlers for the control’s interaction are defined on the entire control surface, not on each image. So when an image is tapped or clicked, we need to detect which one was actually hit. We then set the control to a new value which is rounded to the integer, so the whole tapped/clicked images becomes selected:

private void Surface_Tapped(object sender, TappedRoutedEventArgs e)
{
    if (!IsInteractive)
    {
        return;
    }

    Value = (int)(e.GetPosition(this).X / (ActualWidth + ItemPadding) * Maximum) + 1;
}

The calculation for deriving the Value from the the horizontal sliding manipulation is a bit more complex because we want the ‘curtain’ to closely follow the finger/pointer. We don’t change the control’s Value while sliding between the images, which creates a very natural user experience:

private void Surface_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    if (!IsInteractive)
    {
        return;
    }

    // Floor.
    var value = Math.Floor(e.Position.X / (ActualWidth + ItemPadding) * Maximum);

    // Step.
    value += Math.Min(RoundToFraction(
	((e.Position.X - (ItemHeight + ItemPadding) * (value)) / (ItemHeight)), StepFrequency), 1);

    // Keep within range.
    if (value < 0)
    {
        value = 0;
    }
    else if (value > Maximum)
    {
        value = Maximum;
    }

    Value = value;
}

Using the Rating Control

When you want to use the rating control in your app, just declare its namespace in the XAML:

xmlns:controls="using:XamlBrewer.Uwp.Controls"

Then draw a Rating control and set its properties – as already mentioned: all of the properties have a default value:

<controls:Rating x:Name="Devils"
                    Maximum="4"
                    ItemHeight="60"
                    ItemPadding="24"
                    StepFrequency=".1"
                    EmptyImage="ms-appx:///Assets/RatingImages/devil_empty.png"
                    FilledImage="ms-appx:///Assets/RatingImages/devil_full.png" />

That’s all there is.

It’s a UWP control, so it runs on PC’s, tablets, Raspberry Pi, Xbox, and Hololens. Since I don’t own all of these (yet), here’s a screenshot from the phone:

RatingOnPhone

Source code

The XAML-and-Composition Rating Control for UWP lives here on GitHub, together with the sample app.

Enjoy!

UWP Composition Effects: Chaining

The number of UWP Composition Effects is relatively limited -especially compared to the total list of Win2D effects. But you can roll your own custom effect by building an effect factory that combines two or more effects.  In this short article we’ll explain how a XAML UWP app can combine –or chain- multiple Composition Effects on a single image, and update properties on (one of) these effects dynamically.

Here are some screenshots of the corresponding sample app.

  • The image on the left shows a configurable SaturationEffect. This effect allows you adapt the saturation of the image – how far the colors are from any shade of grey. The saturation can be changed by the slider underneath.
  • The middle image shows the result of an InvertEffect that … inverts the colors of the same base image.
  • The image on the right combines the two effects:

Chaining0

Chaining1

The steps to load an image, apply any of the Composition Effects on it and display the result inside a XAML element, were explained in previous blog posts. So allow me to fast-forward through the process. The same steps are also explained on MSDN, but the sample app executes them in a totally different order. As long as you build a decent effect graph, it will display properly.

Here’s how the sample app does it:

_leftSpriteVisual = _compositor.CreateSpriteVisual();
var side = (float)Math.Min(LeftPresenter.ActualWidth, LeftPresenter.ActualHeight);
_leftSpriteVisual.Size = new Vector2(side, side);
_leftRoot.Children.InsertAtTop(_leftSpriteVisual);
// Create CompositionSurfaceBrush
_imageBrush = _compositor.CreateSurfaceBrush();

// Create an image source to load
var imageSource = _imageFactory.CreateImageFromUri(
	new Uri("ms-appx:///Assets/flowers.jpg"));
_imageBrush.Surface = imageSource.Surface;

Here’s the code for the saturation effect, with the saturation value as a dynamic property:

// Create and apply the first effect.
_firstEffect = new SaturationEffect
{
    Name = "firstEffect",
    Source = new CompositionEffectSourceParameter("source")
};
var firstEffectFactory = _compositor.CreateEffectFactory(
	_firstEffect, 
	new[] { "firstEffect.Saturation" });
_effectBrush1 = firstEffectFactory.CreateBrush();
_leftSpriteVisual.Brush = _effectBrush1;

Here’s the code for the invert effect, without any configuration:

// Create and apply the second effect.
var secondEffect = new InvertEffect
{
    Name = "secondEffect",
    Source = new CompositionEffectSourceParameter("source")
};
var secondEffectFactory = _compositor.CreateEffectFactory(secondEffect);
_effectBrush2 = secondEffectFactory.CreateBrush();
_middleSpriteVisual.Brush = _effectBrush2;

And finally, here’s the code to create the combined effect. Observe that it is just an InvertEffect that takes the SaturationEffect as Source:

// Create and apply the combined effect.
_combinedEffect = new InvertEffect
{
    Name = "chained",
    Source = _firstEffect
};
var combinedEffectFactory = _compositor.CreateEffectFactory(
	_combinedEffect, 
	new[] { "firstEffect.Saturation" });
_combinedBrush = combinedEffectFactory.CreateBrush();
_rightSpriteVisual.Brush = _combinedBrush;

Observe that we reuse the same image brush instance in all three images:

// Chain the brushes
_effectBrush1.SetSourceParameter("source", _imageBrush);
_effectBrush2.SetSourceParameter("source", _imageBrush);
_combinedBrush.SetSourceParameter("source", _imageBrush);

The image that is loaded into the first effect (the source of the combined effect) is tied to the first effect factory, NOT the combined effect factory. So you do have to load the image into the combined brush.

  • Update the dynamic properties. These are the properties that you will set on-the-fly, with or without animation. Here’s the code behind the slider:
private void Slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
    if (_effectBrush1 != null)
    {
        // Apply parameter to brushes.
        _effectBrush1.Properties.InsertScalar(
	"firstEffect.Saturation", 
	(float)e.NewValue);
        _combinedBrush.Properties.InsertScalar(
	"firstEffect.Saturation", 
	(float)e.NewValue);
    }
}

Again, the dynamic properties for the first effect brush are tied to the first effect factory, and hence do not apply to the combined effect. So you have to update the dynamic properties on the combined brush. The sample app needs to update two brushes, because it displays the two separate images.

That app lives here on GitHub.

Enjoy!

UWP Composition Effects: Temperature and Tint

In this article we discuss TemperatureAndTint, the Composition API effect that -unsurprisingly- allows you to adjust the temperature and/or tint of an image.

The Temperature property affects the balance between the blue and yellow colors in an image. Decreasing the color temperature makes an image look bluish. Increasing the temperature intensifies the green and red colors – in RGB-space this is the way to increase yellow. Here’s what happens to an image of a color wheel (image in the middle) when you decrease (image on the left) or increase (image on the right) the color temperature:

Temperature1 Temperature2 Temperature3

The Temperature property of the TemperatureAndTint effect does not stand for the color temperature of the image itself, for the relative change in color that is caused by the effect. It is defined as a float between –1 and 1.

The Tint property affects the balance between the green and magenta colors in an image. Increasing the tint increases the intensity of red and blue pixels, while decreasing increases the amount of green. Here’s the effect of changing the Tint in a color wheel image:

Tint1 Temperature2 Tint2

The Tint property is also a value in the range from -1 to 1.

What is Color Temperature?

The standard unit measure of Color Temperature is degrees Kelvin (K) -a variation of Centigrade a.k.a. degrees Celsius- and it ranges from 1000 to 10000. It was discovered in the late 1800’s at a Scottish barbecue. Physicist and mathematician Lord William Kelvin heated a block of carbon and observed its glow producing a range of different colors at different temperatures. The heated black cube first produced a dim red light, increasing to a brighter yellow as the temperature went up, and eventually produced a bright blue glow at the highest temperatures. These observations led to a color temperature scale based on a theoretical “ideal black body”.

Funny enough, the bluish light -which corresponds to the higher color temperature- is described as cool. So warm and cool refer to the color, not to the temperature.

Here’s how Kelvin’s color temperature scale looks like:

ColorTemperatures

Most computer screens and televisions have a color temperature of 6500 K. That’s why you see a blue glow through the windows when you drive by someone’s house at night.

Let’s see how we can use the notion of Color Temperature on the Universal Windows Platform, and find some use cases for it.

How to apply the effect with the Composition API

In Visual Studio, make sure that your UWP project references the Microsoft UI Composition Toolkit project, and install the Win2D UWP Nuget package. For more info on the configuration and initialization of the different Composition API citizens, read my previous article.

When we create the effect, we give it a name. When we create an EffectFactory for the effect, we parameterize the Temperature and Tint properties:

// Create the effect, but don't specify the properties yet.
var temperatureAndTintEffect = new TemperatureAndTintEffect
{
    Name = "temperatureAndtint",
    Source = new CompositionEffectSourceParameter("source")
};

// Compile the effect
var effectFactory = _compositor.CreateEffectFactory(
    temperatureAndTintEffect,
    new[] { "temperatureAndtint.Temperature", "temperatureAndtint.Tint" });

Then we create a brush and apply it to the SpriteVisual:

// Create and apply the brush.
_brush = effectFactory.CreateBrush();
_spriteVisual.Brush = _brush;

There’s no base image loaded yet, so here’s how to load one in a CompositionSurfaceBrush  and plug that brush in the effect brush:

// Create CompositionSurfaceBrush
var surfaceBrush = _compositor.CreateSurfaceBrush();

// Create an image source to load
CompositionImage imageSource = _imageFactory.CreateImageFromUri(uri);
surfaceBrush.Surface = imageSource.Surface;

_brush.SetSourceParameter("source", surfaceBrush);

When you know the values that you want to apply to the Temperature and/or Tint properties of the effect (e.g. from a Slider control or data binding), then call InsertScalar:

private void ChangeTemperature(float temperature)
{
    // Apply parameter to brush.
    _brush.Properties.InsertScalar("temperatureAndtint.Temperature", temperature);
}

private void ChangeTint(float tint)
{
    // Apply parameter to brush.
    _brush.Properties.InsertScalar("temperatureAndtint.Tint", tint);
}

warning_thumb3  Tip: You can avoid hardcoded strings by using the nameof() operator.

Some of the Composition API effect related methods require parameters in the name-of-the-effect.name-of-the-property format, like “temperatureAndTint.Temperature”. With the nameof() operator you can get to the name of a variable, type, or member at runtime. That allows you to avoid the hard-coded strings to better survive refactorings.

Here’s how you could store the Temperature parameter and use it in the EffectFactory constructor:

// Strongly typed version of the "temperatureAndtint.Temperature" string
_temperatureParameter = temperatureAndTintEffect.Name 
	+ "." 
	+ nameof(temperatureAndTintEffect.Temperature);
var effectFactory = _compositor.CreateEffectFactory(
    temperatureAndTintEffect,
    new[] { _temperatureParameter, "temperatureAndtint.Tint" });

The value was stored in a field, so it can be reused when we apply the value:

_brush.Properties.InsertScalar(_temperatureParameter, temperature);

Using Color Temperature to adjust the lighting in photos

The most common use of Color Temperature in software is white-balancing pictures. Our brain automatically adapts to different lighting colors: we perceive a white T-shirt as white whether we are indoors and using warm artificial lighting or outdoors in sunlight. Our eyes and brain recalibrate what we read as white, based on our experience and points of reference, but a camera does not. When taking a picture, the camera needs to be properly configured to the correct color temperature setting. If it’s not well done, the image needs to post-processed.

Many photo editor apps come with a slider for the Temperature in the Red-White-Blue scale, and a slider for the Tint in the Green to Magenta scale to do minor adjustments.

That’s exactly what the sample app does, so let’s move to other usages of Color Temperature.

Using Color Temperature to adapt a display to the time of day

The TemperatureAndTint effects provides you with an opportunity to make text better readable and interacting with your app more comfortable and less fatiguing. If you are reading a book, the pages will always appear white – no matter what light source you’re under. But pure white is not always the ideal background. For some apps, it makes sense to adjust the background color based on your user’s environment (indoors or outdoors) or the time of day (day or night): blue is a better background in full sunlight, and soft yellow-to-orange tones are better for reading bed-time stories. There already is a huge range of apps available that monitor the light conditions (or use history data) and adapt the screen setting accordingly. For a good example, take a look at f.lux.

Apps on the Universal Windows Platforms have no write access to screen settings and system colors. But nothing prevents you from using a pale background image in your app and changing its color temperature through the Composition API.

Here’s what happens when you load a white gradient bitmap into the sample app and change the color temperature:

White1 White2 White3

Using Color Temperature to highlight an area

The Color Temperature effect is not only useful on a white background. The following screenshots show the effect in action against a colorful (blue and red) background:

Colorful1 Colorful2 Colorful3

In the image on the left, the blue area is nicely highlighted. In the image on the right, the red area gets all the attention. So adjusting the Color Temperature (or Tint) can be used to temporarily highlight different zones in your UWP app, and this can be done with animation.

The Code

The sample app lives here on GitHub. The Assets folder in the main project has other sample images for you to experiment with: people, flowers, buildings…

Enjoy!

UWP Composition Effects: Hue Rotation

In this article we play around with the Hue Rotation effect, which is one of the UWP Composition Effects. This effect allows you to apply a rotation to all of the colors of an image in real-time. The effect takes one parameter -the rotation angle- which is a float value between 0 and 2π.

The best way to demonstrate the Hue Rotation, is to apply it on an image of a color wheel. By changing the angle of the effect, the image seems to rotate physically. Here are some screenshots of the sample app that I wrote. It applies the Hue Rotation effect on some images. The rotation angle of the effect is controlled by a slider:

ColorWheel_1

ColorWheel_2

ColorWheel_3

When you move the slider slowly from its minimum to its maximum value, you’ll see that the image appears to rotate clockwise. It looks like a RotateTransform, but we’re actually just shifting the colors of the bitmap. The color shift is not linear however: for some rotation angles the yellow range entirely disappears – like in the screenshot in the middle. That’s because in color theory nothing is linear: everything is based on non-linear physical characteristics as well as on biological ones (how the human eye works). There is an awesome and well-readable introduction to all of this right here.

So rotating colors may look easy at first sight, but it’s definitely not. The Hue Rotation effect actually applies a color matrix in the red-green-blue space. The matrix depends on the rotation angle.

Enough introduction, let’s fire up Visual Studio now.

Preparing the project

The core components of the Composition API are built into the UWP platform. On top of that, the sample app makes use of the Microsoft UI Composition Toolkit. That’s a small helper library, written in C++, that facilitates loading bitmap images into composition visuals:

CompositionToolkit

When you want to use one or more of the Composition API effects in your UWP app, then you also need to add the Win2D UWP Nuget package:

Win2D_uwp_Nuget

One line of XAML

The Composition API sits between XAML and DirecX, so XAML-wise there is not much to do: it suffices to define a host element for the image. A named Grid control will do:

<Grid x:Name="Container" Margin="0" />

Creating and applying the effect

For the C# part, we start with defining some fields in the View’s code-behind. We define

private Compositor _compositor;
private ContainerVisual _root;
private CompositionImageFactory _imageFactory;
private SpriteVisual _spriteVisual;
private CompositionEffectBrush _brush;

When the page is loaded, we initialize the infrastructure:

// Initialize Composition UI infrastructure.
_root = Container.GetVisual();
_compositor = _root.Compositor;
_imageFactory = 
	CompositionImageFactory.CreateCompositionImageFactory(_compositor);

We make use of the following extension method to create the placeholder:

public static class UIElementExtensions
{
    public static ContainerVisual GetVisual(this UIElement element)
    {
        var hostVisual = ElementCompositionPreview.GetElementVisual(element);
        var root = hostVisual.Compositor.CreateContainerVisual();
        ElementCompositionPreview.SetElementChildVisual(element, root);
        return root;
    }
}

Here’s how to create the sprite visual, and hook it into the XAML visual tree:

// Hook the sprite visual into the XAML visual tree.
_spriteVisual = _compositor.CreateSpriteVisual();
var side = (float)Math.Min(
	Presenter.ActualWidth, Presenter.ActualHeight);
_spriteVisual.Size = new Vector2(side, side);
_root.Children.InsertAtTop(_spriteVisual);

All the previous code is further explained in this article of mine on the basics of the Composition API.

Let’s now dive into the effect related code. The next couple of steps create the Hue Rotation effect:

  • We first define the effect, where we specify the name of the image brush that will be used as Source, but we omit the Angle.
  • Then we compile it with CreateEffectFactory() where we specify the parameterized properties (i.c. the Angle) in an EffectName.PropertyName syntax, and
  • create a brush that holds the effect, through CreateBrush().
  • Finally we apply that brush to the sprite visual:
// Create the effect, but don't specify the Angle yet.
var hueRotationEffect = new HueRotationEffect
{
    Name = "hueRotation",
    Source = new CompositionEffectSourceParameter("source")
};

// Compile the effect
var effectFactory = _compositor.CreateEffectFactory(
	hueRotationEffect, 
	new[] { "hueRotation.Angle" });

// Create and apply the brush.
_brush = effectFactory.CreateBrush();
_spriteVisual.Brush = _brush;

An effect needs pixels to work on, so it is always chained to a CompositionSurfaceBrush which can hold a color, a bitmap, or an effect.

Here’s how to load a bitmap into a brush:

private void LoadImage(Uri uri)
{
    // Create CompositionSurfaceBrush
    var surfaceBrush = _compositor.CreateSurfaceBrush();

    // Create an image source to load
    CompositionImage imageSource = _imageFactory.CreateImageFromUri(uri);
    surfaceBrush.Surface = imageSource.Surface;

    _brush.SetSourceParameter("source", surfaceBrush);
}

The last line of the previous snippet  –the SetSourceParameter() call- connects the effect brush to the image brush.

When we created the effect, we parameterized the hue rotation angle. This allows animation and programmatic access. When the rotation angle changes, there’s no need to reload or redraw the bitmap. We only need to apply a new value to the angle. :

private void RotateHue(float angle)
{
    // Apply parameter to brush.
    _brush.Properties.InsertScalar("hueRotation.Angle", angle);
}

We now have a nice playground to test the effect on different images.

What about black and white?

The Hue Rotation effect should not recolor black, grey, or white pixels, since these are not on the color wheel. And indeed, when we load a black and white gradient image into the sprite visual and apply the effect, nothing changes:

BlackAndWhite

A real life example

Here’s an example of where the Hue Rotation effect could be used in an app. Let’s take a look at the Color Picker from WinRT XAML Toolkit in action:

xamlcolorpicker

For a human it’s hard to find or define a color by just using sliders for the amount of red, green, and blue (RGB). Most color pickers use another standard to describe the same set of colors: HSL or HSV, where

  • H stands for Hue. This is the color’s position on the color wheel, generally expressed in degrees from 0° to 359° where 0° represents red and 180° corresponds to red’s opposite color – cyan.
  • S stands for Saturation, the purity of the color, or how far it is from any shade of grey. It is expressed as a percentage.
  • L and V stand for Lightness and Value (a.k.a Brightness). These are two different ways of specifying how far away the color is from black. Both are expressed as a percentage.

For more definitions (chroma, luma, luminance, colorfulness and so on) and more exact definitions and formulas, start reading here.

The color wheel on the outside of the color picker, allows you to easily pick the base color (the Hue). The triangle on the inside manages the two other properties: Brightness and Saturation.

If you dive into the code of the WinRT XAML Toolkit Color Picker Sample, you’ll notice that after every Hue change, the inner triangle is entirely redrawn pixel by pixel. The control  calls the RenderColorPickerSaturationLightnessTriangleCore() method in WriteableBitmapColorPickerExtensions. When the size of the control changes, the same algorithm is applied and everything is redrawn pixel by pixel.

All in all, that’s a pretty heavy operation, and I can imagine that some hardware –like a cheap phone or an IoT device- will choke on this.

This is a scenario where the Composition API and the Hue Rotation effect in particular can come to the rescue. The following set of screenshots show the impact of Hue Rotation on a monochromatic image. The main color of the image is changed but the white, black, and grey tones are unaffected, and that’s exactly what the color picker needs:

BrightnessAndSaturation_1

BrightnessAndSaturation_2

BrightnessAndSaturation_3

The image is never redrawn, only its colors are shifted. I can confirm that it runs smoothly on the phone:

HueRotationPhone

The code

The sample app is here on GitHub. It will be extended with demos of other Composition API effects.

Enjoy!