Skip to content

Commit

Permalink
Merge pull request #196 from Geodan/use_rgba_hexcodes
Browse files Browse the repository at this point in the history
use rgba hexcodes not argb
  • Loading branch information
bertt authored Aug 15, 2024
2 parents 17099af + 0250c34 commit 1e71555
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ If --username and/or --dbname are not specified the current username is used as
--copyright (Default: '') glTF asset copyright
--default_color (Default: #FFFFFF) Default color, in (A)RGB order
--default_color (Default: #FFFFFF) Default color, in RGB(A) order
--default_metallic_roughness (Default: #008000) Default metallic roughness
Expand Down
14 changes: 7 additions & 7 deletions src/wkb2gltf.core.tests/ColorTests.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
using System.Drawing;
using NUnit.Framework;
using NUnit.Framework;

namespace Wkb2Gltf.Tests;

public class ColorTests
{

[Test]
public void HexColorToRgbTest()
{
var hexcolor = "#ff5555";
var color = ColorTranslator.FromHtml(hexcolor);
Assert.That(color.R == 255 && color.G == 85 && color.B == 85, Is.True);
var color = RgbaColor.FromHex(hexcolor);
Assert.That(color.R == 255 && color.G == 85 && color.B == 85);
}

[Test]
public void HexColorWithAlphaToRgbaTest()
{
var hexcolor = "#55ff5657";
var color = ColorTranslator.FromHtml(hexcolor);
Assert.That(color.A == 85 && color.R == 255 && color.G == 86 && color.B == 87, Is.True);
var hexcolor = "#FF000055";
var color = RgbaColor.FromHex(hexcolor);
Assert.That(color.A == 85 && color.R == 255 && color.G == 0 && color.B == 0);
}
}
24 changes: 10 additions & 14 deletions src/wkb2gltf.core.tests/MaterialCreatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@ public class MaterialCreatorTests
[Test]
public void ColorConvertTest()
{
var hex = "#E6008000";
var color = ColorTranslator.FromHtml(hex);
Assert.That(color.A == 230, Is.True);
Assert.That(color.R == 0, Is.True);
Assert.That(color.G == 128, Is.True);
var hex = "#FF000055";
var color = RgbaColor.FromHex(hex);
Assert.That(color.A == 85);
Assert.That(color.R == 255, Is.True);
Assert.That(color.G == 0, Is.True);
Assert.That(color.B == 0, Is.True);
var hex2 = ColorTranslator.ToHtml(color);
Assert.That(hex.EndsWith(hex2.Substring(1, hex2.Length - 1)), Is.True);
}

[Test]
public void MetallicRoughnessBaseColorTest()
{
// arrange
var shader = new Shader();
// #008000 = green https://www.color-hex.com/color/008000 (0,128,0)), 81 as Alpha value
shader.PbrMetallicRoughness = new PbrMetallicRoughness() { BaseColor = "#81008000", MetallicRoughness = "" };

shader.PbrMetallicRoughness = new PbrMetallicRoughness() { BaseColor = "#00800081", MetallicRoughness = "" };

// act
var material = MaterialCreator.CreateMaterial(shader, true, AlphaMode.BLEND);
Expand All @@ -55,7 +51,7 @@ public void MetallicRoughnessBaseColorEmissiveTest()
var emissive = new Vector3(10, 20, 30);
// #008000 = green https://www.color-hex.com/color/008000 (0,128,0))
shader.PbrMetallicRoughness = new PbrMetallicRoughness() { BaseColor = "#008000" };
shader.EmissiveColor = ColorTranslator.ToHtml(Color.FromArgb(10, 20, 30));
shader.EmissiveColor = RgbaColor.ToHex(Color.FromArgb(10, 20, 30));
// act
var material = MaterialCreator.CreateMaterial(shader);

Expand Down Expand Up @@ -85,7 +81,7 @@ public void MetallicRoughnessTest()
var roughnessFactor = 0.4f;

var c = Color.FromArgb((int)(metallicFactor * 255), ((int)(roughnessFactor * 255)), 0);
var hex = ColorTranslator.ToHtml(c);
var hex = RgbaColor.ToHex(c);

shader.PbrMetallicRoughness = new PbrMetallicRoughness() { BaseColor = "#008000", MetallicRoughness = hex };

Expand All @@ -108,7 +104,7 @@ public void SpecularGlossinessWithDiffuseTest()
var shader = new Shader();
var diffuse = Color.FromArgb(10, 20, 30, 40); // diffuse rgb + alpha
var specularGlossiness = Color.FromArgb(50, 60, 70, 80); // specular red, green, blue + glossiness
shader.PbrSpecularGlossiness = new PbrSpecularGlossiness() { DiffuseColor = ColorTranslator.ToHtml(diffuse), SpecularGlossiness = ColorTranslator.ToHtml(specularGlossiness) };
shader.PbrSpecularGlossiness = new PbrSpecularGlossiness() { DiffuseColor = RgbaColor.ToHex(diffuse), SpecularGlossiness = RgbaColor.ToHex(specularGlossiness) };
// act
var material = MaterialCreator.CreateMaterial(shader);

Expand All @@ -124,7 +120,7 @@ public void SpecularGlossinessTest()
{
var shader = new Shader();
var specularGlossiness = Color.FromArgb(50, 60, 70, 80); // specular red, green, blue + glossiness
shader.PbrSpecularGlossiness = new PbrSpecularGlossiness() { SpecularGlossiness = ColorTranslator.ToHtml(specularGlossiness) };
shader.PbrSpecularGlossiness = new PbrSpecularGlossiness() { SpecularGlossiness = RgbaColor.ToHex(specularGlossiness) };
// act
var material = MaterialCreator.CreateMaterial(shader);

Expand Down
10 changes: 5 additions & 5 deletions src/wkb2gltf.core/MaterialCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static MaterialBuilder CreateMaterial(Shader shader, bool defaultDoubleSi
WithAlpha(defaultAlphaMode);

if (!string.IsNullOrEmpty(shader.EmissiveColor)) {
material.WithEmissive(ColorToVector3(ColorTranslator.FromHtml(shader.EmissiveColor)));
material.WithEmissive(ColorToVector3(RgbaColor.FromHex(shader.EmissiveColor)));
}
if (shader.PbrSpecularGlossiness != null) {
#pragma warning disable CS0618 // Type or member is obsolete
Expand All @@ -22,11 +22,11 @@ public static MaterialBuilder CreateMaterial(Shader shader, bool defaultDoubleSi

if (!string.IsNullOrEmpty(shader.PbrSpecularGlossiness.DiffuseColor)) {
#pragma warning disable CS0618 // Type or member is obsolete
material.WithDiffuse(ColorToVector4(ColorTranslator.FromHtml(shader.PbrSpecularGlossiness.DiffuseColor)));
material.WithDiffuse(ColorToVector4(RgbaColor.FromHex(shader.PbrSpecularGlossiness.DiffuseColor)));
#pragma warning restore CS0618 // Type or member is obsolete
}
if (!string.IsNullOrEmpty(shader.PbrSpecularGlossiness.SpecularGlossiness)) {
var c = ColorToVector4(ColorTranslator.FromHtml(shader.PbrSpecularGlossiness.SpecularGlossiness));
var c = ColorToVector4(RgbaColor.FromHex(shader.PbrSpecularGlossiness.SpecularGlossiness));
var specular = new Vector3(c.X, c.Y, c.Z);
var glossiness = c.Z;
#pragma warning disable CS0618 // Type or member is obsolete
Expand All @@ -37,11 +37,11 @@ public static MaterialBuilder CreateMaterial(Shader shader, bool defaultDoubleSi
else if (shader.PbrMetallicRoughness != null) {
material.WithMetallicRoughnessShader();
if (!string.IsNullOrEmpty(shader.PbrMetallicRoughness.BaseColor)) {
material.WithBaseColor(ColorToVector4(ColorTranslator.FromHtml(shader.PbrMetallicRoughness.BaseColor)));
material.WithBaseColor(ColorToVector4(RgbaColor.FromHex(shader.PbrMetallicRoughness.BaseColor)));
}

if (!string.IsNullOrEmpty(shader.PbrMetallicRoughness.MetallicRoughness)) {
var html = ColorTranslator.FromHtml(shader.PbrMetallicRoughness.MetallicRoughness);
var html = RgbaColor.FromHex(shader.PbrMetallicRoughness.MetallicRoughness);
var c = ColorToVector4(html);
material.WithMetallicRoughness(c.X, c.Y);
}
Expand Down
42 changes: 42 additions & 0 deletions src/wkb2gltf.core/RgabColor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Drawing;
using System.Globalization;

namespace Wkb2Gltf;

public static class RgbaColor
{
public static string ToHex(Color color)
{
return $"#{color.R:X2}{color.G:X2}{color.B:X2}{color.A:X2}";
}
public static Color FromHex(string hex)
{
var hexSpan = hex.AsSpan();

if (hexSpan.StartsWith("#")) {
hexSpan = hexSpan.Slice(1);
}

var rgb = GetRgb(hexSpan);

if (hexSpan.Length == 8) {
var alpha = byte.Parse(hexSpan.Slice(6, 2), NumberStyles.HexNumber);
return Color.FromArgb(alpha, rgb.red, rgb.green, rgb.blue);
}
else if (hexSpan.Length == 6) {
return Color.FromArgb(255, rgb.red, rgb.green, rgb.blue);
}
else {
throw new ArgumentException("Hex-color code must be 6 or 8 characters.");
}
}

private static (int red, int green, int blue) GetRgb(ReadOnlySpan<char> hexSpan)
{
var red = byte.Parse(hexSpan.Slice(0, 2), NumberStyles.HexNumber);
var green = byte.Parse(hexSpan.Slice(2, 2), NumberStyles.HexNumber);
var blue = byte.Parse(hexSpan.Slice(4, 2), NumberStyles.HexNumber);
return (red, green, blue);
}
}
8 changes: 6 additions & 2 deletions styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ Remember to add attribute bouwjaar with '-a bouwjaar' when creating the 3D Tiles
## Server side styling

When using server side styling the 3D Tiles are styled in the database.
The styling is stored in a json document in a column of type json.

The styling is stored in a json document in a column of type json.

A hex color notation is used for the colors, in the form of #RRGGBBAA. Sample:

Hex code #FF000055 is Color red (r,g,b = 255,0,0) + Alpha = 85

The json document contains the shaders for the 3D Tiles. When the option --shaderscolumn is used, the shaders are read from the column specified in the option.

Expand All @@ -62,7 +67,6 @@ Metallic factor: 0, Roughness factor: 0.5019608 (128/255)

- Doubleside: true (option double_sided)


Alternative option is to specify a shader using the ShadersColumn.

Shaderscolumn is a column of type json. In the json documents the shaders are defined like PbrMetallicRoughness and
Expand Down

0 comments on commit 1e71555

Please sign in to comment.