Skip to content

Commit

Permalink
add - doc - Added web-safe color list
Browse files Browse the repository at this point in the history
---

We've added a list of web-safe colors!

---

Type: add
Breaking: False
Doc Required: True
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Jul 2, 2024
1 parent cf7b15e commit 0a6b01b
Show file tree
Hide file tree
Showing 4 changed files with 2,493 additions and 0 deletions.
141 changes: 141 additions & 0 deletions Terminaux.ColorDataGen/ColorPropGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -30,6 +31,7 @@ namespace Terminaux.ColorDataGen
public class ColorPropGen : ISourceGenerator
{
private string colorContent = "";
private string webSafeColorsContent = "";

public void Execute(GeneratorExecutionContext context)
{
Expand All @@ -42,6 +44,14 @@ public void Execute(GeneratorExecutionContext context)
// Now, populate the color enumerations and data properties
PopulateColorEnums(context);
PopulateColorData(context);

// Get the other color data
var webSafeStream = asm.GetManifestResourceStream($"{asm.GetName().Name}.Resources.WebSafeColors.json");
using var webSafeReader = new StreamReader(webSafeStream);
webSafeColorsContent = webSafeReader.ReadToEnd();

// Now, populate the other color properties
PopulateWebSafeColorData(context);
}

public void Initialize(GeneratorInitializationContext context)
Expand Down Expand Up @@ -208,5 +218,136 @@ public enum ConsoleColors
builder.AppendLine(footer);
context.AddSource("ConsoleColors.cs", builder.ToString());
}

private void PopulateWebSafeColorData(GeneratorExecutionContext context)
{
string header =
$$"""
//
// Terminaux Copyright (C) 2023-2024 Aptivi
//
// This file is part of Terminaux
//
// Terminaux is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Terminaux is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

// <auto-generated/>

namespace Terminaux.Colors.Data
{
/// <summary>
/// Web safe color list as specified by W3C.
/// </summary>
public static class WebSafeColors
{
// Taken from https://github.com/jonathantneal/color-names

""";
string footer =
$$"""
}
}
""";
var builder = new StringBuilder(header);

// Read all the console color data
var list = JToken.Parse(webSafeColorsContent);
if (list is null)
return;

// First, populate all the color data properties
foreach (JProperty colorData in list.Cast<JProperty>())
{
string colorHex = colorData.Name;
string colorName = colorData.Value.ToString();
string propName = colorName.Replace(" ", "");
builder.AppendLine(
$$"""
/// <summary>
/// [{{colorHex}}] Gets a color instance for the {{propName}} color
/// </summary>
public static Color {{propName}} =>
new("{{colorHex}}");
"""
);
}
builder.AppendLine();

// Then, populate a function that gets all this data
builder.AppendLine(
"""
/// <summary>
/// Gets the color list
/// </summary>
/// <returns>A color list</returns>
public static Color[] GetColorList()
{
return
[
"""
);
foreach (JProperty colorData in list.Cast<JProperty>())
{
string colorName = colorData.Value.ToString();
string propName = colorName.Replace(" ", "");
builder.AppendLine(
$$"""
{{propName}},
"""
);
}
builder.AppendLine(
"""
];
}
"""
);
builder.AppendLine();

// Then, populate a function that gets all the names
builder.AppendLine(
"""
/// <summary>
/// Gets the websafe color names
/// </summary>
/// <returns>A websafe color names array</returns>
public static string[] GetColorNames()
{
return
[
"""
);
foreach (JProperty colorData in list.Cast<JProperty>())
{
string colorName = colorData.Value.ToString();
string propName = colorName.Replace(" ", "");
builder.AppendLine(
$$"""
"{{propName}}",
"""
);
}
builder.AppendLine(
"""
];
}
"""
);

// Add the source code to the compilation
builder.AppendLine(footer);
context.AddSource("WebSafeColors.g.cs", builder.ToString());
}
}
}
Loading

0 comments on commit 0a6b01b

Please sign in to comment.