Skip to content

Commit f2855be

Browse files
feat: add C/c as cross-type format specifier alias for Color
C/c aliases R/r on Color (CSS rgb output), matching the convention where C/c outputs the natural CSS format for any color type.
1 parent 7adf64c commit f2855be

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

src/Avalonia.Base/Media/Color.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,8 @@ internal void ToString(System.Text.StringBuilder builder)
476476
/// <item><c>"x"</c> — XAML hex without alpha: <c>#RRGGBB</c></item>
477477
/// <item><c>"H"</c> — HTML hex with alpha: <c>#RRGGBBAA</c></item>
478478
/// <item><c>"h"</c> — HTML hex without alpha: <c>#RRGGBB</c></item>
479-
/// <item><c>"R"</c> — CSS rgb with alpha: <c>rgb(r, g, b, a)</c></item>
480-
/// <item><c>"r"</c> — CSS rgb without alpha: <c>rgb(r, g, b)</c></item>
479+
/// <item><c>"R"</c> or <c>"C"</c> — CSS rgb with alpha: <c>rgb(r, g, b, a)</c></item>
480+
/// <item><c>"r"</c> or <c>"c"</c> — CSS rgb without alpha: <c>rgb(r, g, b)</c></item>
481481
/// <item><c>"P"</c> — CSS rgb percent with alpha: <c>rgb(r%, g%, b%, a%)</c></item>
482482
/// <item><c>"p"</c> — CSS rgb percent without alpha: <c>rgb(r%, g%, b%)</c></item>
483483
/// </list>
@@ -498,8 +498,8 @@ public string ToString(string? format, IFormatProvider? formatProvider)
498498
"x" => $"#{R:X2}{G:X2}{B:X2}",
499499
"H" => $"#{R:X2}{G:X2}{B:X2}{A:X2}",
500500
"h" => $"#{R:X2}{G:X2}{B:X2}",
501-
"R" => string.Format(CultureInfo.InvariantCulture, "rgb({0}, {1}, {2}, {3:F2})", R, G, B, A * byteToDouble),
502-
"r" => string.Format(CultureInfo.InvariantCulture, "rgb({0}, {1}, {2})", R, G, B),
501+
"R" or "C" => string.Format(CultureInfo.InvariantCulture, "rgb({0}, {1}, {2}, {3:F2})", R, G, B, A * byteToDouble),
502+
"r" or "c" => string.Format(CultureInfo.InvariantCulture, "rgb({0}, {1}, {2})", R, G, B),
503503
"P" => FormatCssRgbPercentWithAlpha(),
504504
"p" => FormatCssRgbPercentWithoutAlpha(),
505505
_ => throw new FormatException($"Format string '{format}' is not supported.")

tests/Avalonia.Base.UnitTests/Media/ColorTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,5 +515,14 @@ public void ToString_x_And_h_Produce_Same_Output()
515515
color.ToString("x", null),
516516
color.ToString("h", null));
517517
}
518+
519+
[Fact]
520+
public void ToString_C_Is_Alias_For_R()
521+
{
522+
var color = new Color(0x80, 0xFF, 0x88, 0x44);
523+
524+
Assert.Equal(color.ToString("R", null), color.ToString("C", null));
525+
Assert.Equal(color.ToString("r", null), color.ToString("c", null));
526+
}
518527
}
519528
}

0 commit comments

Comments
 (0)