Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
96aeba6
Created a coveter for hex, to Maui Color
eduardoagr Nov 20, 2023
86463b5
Change from BaseConverterOneWay to BaseConverter
eduardoagr Nov 20, 2023
f425e6e
Merge branch 'main' into main
TheCodeTraveler Nov 20, 2023
ed9ee77
Merge branch 'main' into main
TheCodeTraveler Nov 21, 2023
dcd88d7
better performance inn the converter
eduardoagr Nov 27, 2023
552ee52
Merge branch 'main' of https://github.com/eduardoagr/Maui.RGB-TO-COLOR
eduardoagr Nov 27, 2023
b0eea26
Add ConvertBack test
bijington Nov 29, 2023
8540aa1
formatted
eduardoagr Nov 29, 2023
9ebb1ed
Revert "formatted"
bijington Nov 29, 2023
bd52411
Tidy up formatting
bijington Nov 29, 2023
8d550b0
Merge branch 'CommunityToolkit:main' into main
eduardoagr Nov 29, 2023
e36d7a7
Merge branch 'main' into main
TheCodeTraveler Feb 2, 2024
dcb7771
Merge branch 'main' into main
pictos Feb 2, 2024
2072171
Make ColorToRgbaStringConverter two-way
bijington Feb 11, 2024
bbfb43a
Merge branch 'main' of https://github.com/eduardoagr/Maui.RGB-TO-COLO…
bijington Feb 11, 2024
2824001
Format fix
bijington Feb 11, 2024
1535d13
Format fix
bijington Feb 11, 2024
c0bb201
Format fix
bijington Feb 11, 2024
c1560d2
Merge branch 'main' into main
TheCodeTraveler Feb 15, 2024
1c6c18b
Merge branch 'main' into main
bijington Feb 15, 2024
32d947f
Correct formatting
bijington Feb 15, 2024
e512485
Merge branch 'main' of https://github.com/eduardoagr/Maui.RGB-TO-COLO…
bijington Feb 15, 2024
f166d9c
Merge branch 'main' into main
bijington Feb 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.Globalization;

using CommunityToolkit.Maui.Converters;

using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Converters;

public abstract class BaseOneWayConverterTest<TConverter> : ConverterTest<TConverter> where TConverter : ICommunityToolkitValueConverter, new()
{
public abstract class BaseOneWayConverterTest<TConverter> : ConverterTest<TConverter> where TConverter : ICommunityToolkitValueConverter, new() {
Comment thread
bijington marked this conversation as resolved.
Outdated
[Fact]
public void ConvertBack_ShouldThrowNotSupportedException()
{
public void ConvertBack_ShouldThrowNotSupportedException() {
var options = new Options();
options.SetShouldSuppressExceptionsInConverters(true);

Expand All @@ -18,11 +18,9 @@ public void ConvertBack_ShouldThrowNotSupportedException()
}
}

public abstract class BaseConverterTest<TConverter> : ConverterTest<TConverter> where TConverter : ICommunityToolkitValueConverter, new()
{
public abstract class BaseConverterTest<TConverter> : ConverterTest<TConverter> where TConverter : ICommunityToolkitValueConverter, new() {
[Fact]
public void InvalidConvertBackValue_ShouldThrowException()
{
public void InvalidConvertBackValue_ShouldThrowException() {
var options = new Options();
options.SetShouldSuppressExceptionsInConverters(false);

Expand All @@ -32,8 +30,7 @@ public void InvalidConvertBackValue_ShouldThrowException()
}

[Fact]
public void InvalidConvertBackValue_ShouldSuppressExceptionsInConverters_ShouldReturnDefaultConvertValue()
{
public void InvalidConvertBackValue_ShouldSuppressExceptionsInConverters_ShouldReturnDefaultConvertValue() {
var options = new Options();
options.SetShouldSuppressExceptionsInConverters(true);

Expand All @@ -45,11 +42,9 @@ public void InvalidConvertBackValue_ShouldSuppressExceptionsInConverters_ShouldR
}
}

public abstract class ConverterTest<TConverter> : BaseTest where TConverter : ICommunityToolkitValueConverter, new()
{
public abstract class ConverterTest<TConverter> : BaseTest where TConverter : ICommunityToolkitValueConverter, new() {
[Fact]
public void InvalidConvertValue_ShouldThrowException()
{
public void InvalidConvertValue_ShouldThrowException() {
var options = new Options();
options.SetShouldSuppressExceptionsInConverters(false);

Expand All @@ -59,8 +54,7 @@ public void InvalidConvertValue_ShouldThrowException()
}

[Fact]
public void InvalidConverterValue_ShouldSuppressExceptionsInConverters_ShouldReturnDefaultConvertValue()
{
public void InvalidConverterValue_ShouldSuppressExceptionsInConverters_ShouldReturnDefaultConvertValue() {
var options = new Options();
options.SetShouldSuppressExceptionsInConverters(true);

Expand All @@ -75,15 +69,12 @@ public void InvalidConverterValue_ShouldSuppressExceptionsInConverters_ShouldRet
protected virtual object? GetInvalidConvertFromValue() => GetInvalidValue(InitializeConverterForInvalidConverterTests().FromType);
protected virtual TConverter InitializeConverterForInvalidConverterTests() => new();

static object GetInvalidValue(Type type)
{
if (type != typeof(string))
{
static object GetInvalidValue(Type type) {
if (type != typeof(string)) {
return string.Empty;
}

if (type != typeof(bool))
{
if (type != typeof(bool)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
using System.Globalization;

using CommunityToolkit.Maui.Core.Extensions;

namespace CommunityToolkit.Maui.Converters;

/// <summary>
/// Converts the incoming value from <see cref="Color"/> and returns the object of a type <see cref="string"/>.
/// </summary>
public class ColorToRgbStringConverter : BaseConverterOneWay<Color, string>
{
public class ColorToRgbStringConverter : BaseConverter<Color, string> {
/// <inheritdoc/>
public override string DefaultConvertReturnValue { get; set; } = string.Empty;

/// <inheritdoc/>
public override string ConvertFrom(Color value, CultureInfo? culture = null)
{
public override Color DefaultConvertBackReturnValue { get; set; } = Colors.Transparent;

/// <inheritdoc/>
public override Color ConvertBackTo(string value, CultureInfo? culture) {
if (Color.TryParse(value, out Color color)) {
return color;
}

return DefaultConvertBackReturnValue;
}


/// <inheritdoc/>
public override string ConvertFrom(Color value, CultureInfo? culture = null) {
ArgumentNullException.ThrowIfNull(value);
return value.ToRgbString();
}
Expand All @@ -22,14 +34,11 @@ public override string ConvertFrom(Color value, CultureInfo? culture = null)
/// <summary>
/// Converts the incoming value from <see cref="Color"/> and returns the object of a type <see cref="string"/>.
/// </summary>
public class ColorToRgbaStringConverter : BaseConverterOneWay<Color, string>
{
public class ColorToRgbaStringConverter : BaseConverterOneWay<Color, string> {
/// <inheritdoc/>
public override string DefaultConvertReturnValue { get; set; } = string.Empty;

/// <inheritdoc/>
public override string ConvertFrom(Color value, CultureInfo? culture = null)
{
public override string ConvertFrom(Color value, CultureInfo? culture = null) {
ArgumentNullException.ThrowIfNull(value);
return value.ToRgbaString(culture);
}
Expand All @@ -38,24 +47,21 @@ public override string ConvertFrom(Color value, CultureInfo? culture = null)
/// <summary>
/// Converts the incoming value from <see cref="Color"/> and returns the object of a type <see cref="string"/> and virce-versa.
/// </summary>
public class ColorToHexRgbStringConverter : BaseConverter<Color, string>
{
public class ColorToHexRgbStringConverter : BaseConverter<Color, string> {
/// <inheritdoc/>
public override string DefaultConvertReturnValue { get; set; } = string.Empty;

/// <inheritdoc/>
public override Color DefaultConvertBackReturnValue { get; set; } = Colors.Transparent;

/// <inheritdoc/>
public override string ConvertFrom(Color value, CultureInfo? culture = null)
{
public override string ConvertFrom(Color value, CultureInfo? culture = null) {
ArgumentNullException.ThrowIfNull(value);
return value.ToRgbaHex(false);
}

/// <inheritdoc/>
public override Color ConvertBackTo(string value, CultureInfo? culture = null)
{
public override Color ConvertBackTo(string value, CultureInfo? culture = null) {
ArgumentNullException.ThrowIfNull(value);
return Color.FromRgba(value);
}
Expand All @@ -64,24 +70,21 @@ public override Color ConvertBackTo(string value, CultureInfo? culture = null)
/// <summary>
/// Converts the incoming value from <see cref="Color"/> and returns the object of a type <see cref="string"/> and virce-versa.
/// </summary>
public class ColorToHexRgbaStringConverter : BaseConverter<Color, string>
{
public class ColorToHexRgbaStringConverter : BaseConverter<Color, string> {
/// <inheritdoc/>
public override string DefaultConvertReturnValue { get; set; } = string.Empty;

/// <inheritdoc/>
public override Color DefaultConvertBackReturnValue { get; set; } = Colors.Transparent;

/// <inheritdoc/>
public override string ConvertFrom(Color value, CultureInfo? culture = null)
{
public override string ConvertFrom(Color value, CultureInfo? culture = null) {
ArgumentNullException.ThrowIfNull(value);
return value.ToRgbaHex(true);
}

/// <inheritdoc/>
public override Color ConvertBackTo(string value, CultureInfo? culture = null)
{
public override Color ConvertBackTo(string value, CultureInfo? culture = null) {
ArgumentNullException.ThrowIfNull(value);
return Color.FromRgba(value);
}
Expand All @@ -90,14 +93,12 @@ public override Color ConvertBackTo(string value, CultureInfo? culture = null)
/// <summary>
/// Converts the incoming value from <see cref="Color"/> and returns the object of a type <see cref="string"/>.
/// </summary>
public class ColorToCmykStringConverter : BaseConverterOneWay<Color, string>
{
public class ColorToCmykStringConverter : BaseConverterOneWay<Color, string> {
/// <inheritdoc/>
public override string DefaultConvertReturnValue { get; set; } = string.Empty;

/// <inheritdoc/>
public override string ConvertFrom(Color value, CultureInfo? culture = null)
{
public override string ConvertFrom(Color value, CultureInfo? culture = null) {
ArgumentNullException.ThrowIfNull(value);
return value.ToCmykString();
}
Expand All @@ -106,14 +107,12 @@ public override string ConvertFrom(Color value, CultureInfo? culture = null)
/// <summary>
/// Converts the incoming value from <see cref="Color"/> and returns the object of a type <see cref="string"/>.
/// </summary>
public class ColorToCmykaStringConverter : BaseConverterOneWay<Color, string>
{
public class ColorToCmykaStringConverter : BaseConverterOneWay<Color, string> {
/// <inheritdoc/>
public override string DefaultConvertReturnValue { get; set; } = string.Empty;

/// <inheritdoc/>
public override string ConvertFrom(Color value, CultureInfo? culture = null)
{
public override string ConvertFrom(Color value, CultureInfo? culture = null) {
ArgumentNullException.ThrowIfNull(value);
return value.ToCmykaString(culture);
}
Expand All @@ -122,14 +121,12 @@ public override string ConvertFrom(Color value, CultureInfo? culture = null)
/// <summary>
/// Converts the incoming value from <see cref="Color"/> and returns the object of a type <see cref="string"/>.
/// </summary>
public class ColorToHslStringConverter : BaseConverterOneWay<Color, string>
{
public class ColorToHslStringConverter : BaseConverterOneWay<Color, string> {
/// <inheritdoc/>
public override string DefaultConvertReturnValue { get; set; } = string.Empty;

/// <inheritdoc/>
public override string ConvertFrom(Color value, CultureInfo? culture = null)
{
public override string ConvertFrom(Color value, CultureInfo? culture = null) {
ArgumentNullException.ThrowIfNull(value);
return value.ToHslString();
}
Expand All @@ -138,14 +135,12 @@ public override string ConvertFrom(Color value, CultureInfo? culture = null)
/// <summary>
/// Converts the incoming value from <see cref="Color"/> and returns the object of a type <see cref="string"/>.
/// </summary>
public class ColorToHslaStringConverter : BaseConverterOneWay<Color, string>
{
public class ColorToHslaStringConverter : BaseConverterOneWay<Color, string> {
/// <inheritdoc/>
public override string DefaultConvertReturnValue { get; set; } = string.Empty;

/// <inheritdoc/>
public override string ConvertFrom(Color value, CultureInfo? culture = null)
{
public override string ConvertFrom(Color value, CultureInfo? culture = null) {
ArgumentNullException.ThrowIfNull(value);
return value.ToHslaString(culture);
}
Expand Down