public static void AssertContrastRatio(Color foreground, Color background, double minimumContrastRatio)
    {
        const double tollerance = 0.1;

        var ratio = ColorAssist.ContrastRatio(foreground, background);

        Assert.True(ratio >= minimumContrastRatio - tollerance, $"Contrast ratio '{ratio}' is less than {minimumContrastRatio} with a tollerance of 0.1");
    }
Example #2
0
        public async Task PrimaryColor_AdjustToTheme(PrimaryColor primary)
        {
            await using var recorder = new TestRecorder(App);

            await App.InitialzeWithMaterialDesign(BaseTheme.Light, primary, colorAdjustment : new ColorAdjustment());

            IWindow window = await App.CreateWindow <ColorAdjustWindow>();

            await recorder.SaveScreenshot();

            Color windowBackground = await window.GetBackgroundColor();

            IVisualElement themeToggle = await window.GetElement("/ToggleButton");

            IVisualElement largeText = await window.GetElement("/TextBlock[0]");

            IVisualElement smallText = await window.GetElement("/TextBlock[1]");

            await AssertContrastRatio();

            await themeToggle.Click();

            await Wait.For(async() => await window.GetBackgroundColor() != windowBackground);

            await AssertContrastRatio();

            recorder.Success();

            async Task AssertContrastRatio()
            {
                Color largeTextForeground = await largeText.GetForegroundColor();

                Color largeTextBackground = await largeText.GetEffectiveBackground();

                Color smallTextForeground = await smallText.GetForegroundColor();

                Color smallTextBackground = await smallText.GetEffectiveBackground();

                var largeContrastRatio = ColorAssist.ContrastRatio(largeTextForeground, largeTextBackground);

                Assert.True(largeContrastRatio >= MaterialDesignSpec.MinimumContrastLargeText, $"Large font contrast ratio '{largeContrastRatio}' does not meet material design spec {MaterialDesignSpec.MinimumContrastLargeText}");
                var smallContrastRatio = ColorAssist.ContrastRatio(smallTextForeground, smallTextBackground);

                Assert.True(smallContrastRatio >= MaterialDesignSpec.MinimumContrastSmallText, $"Small font contrast ratio '{smallContrastRatio}' does not meet material design spec {MaterialDesignSpec.MinimumContrastSmallText}");
            }
        }