Skip to content

Instantly share code, notes, and snippets.

@tompazourek
Last active January 1, 2021 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tompazourek/72b83e2ca7c5bf822d076748141edb45 to your computer and use it in GitHub Desktop.
Save tompazourek/72b83e2ca7c5bf822d076748141edb45 to your computer and use it in GitHub Desktop.
Colourful convert from xy chromaticity to RGB (sRGB)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Colourful" Version="3.0.0-beta1" />
</ItemGroup>
</Project>
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1.csproj", "{6066BE29-5C11-4957-9A9E-E10D5A9734D8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6066BE29-5C11-4957-9A9E-E10D5A9734D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6066BE29-5C11-4957-9A9E-E10D5A9734D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6066BE29-5C11-4957-9A9E-E10D5A9734D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6066BE29-5C11-4957-9A9E-E10D5A9734D8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ACCE265A-1CFE-4906-97A9-C106C7D25C02}
EndGlobalSection
EndGlobal
using System.Linq;
using Colourful;
namespace ConsoleApp1
{
internal class Program
{
private static readonly IColorConverter<xyChromaticity, RGBColor> ConverterXyToRgb = new ConverterBuilder().Fromxy(Illuminants.D65).ToRGB(RGBWorkingSpaces.sRGB).Build();
private static readonly IColorConverter<RGBColor, xyChromaticity> ConverterRgbToXy = new ConverterBuilder().FromRGB(RGBWorkingSpaces.sRGB).Toxy(Illuminants.D65).Build();
private static void Main()
{
// input xy
var inputXy = new xyChromaticity(.735, .265); // xy [x=0.74, y=0.27]
var outputRgb = ConvertXyToRgb(inputXy); // RGB [R=1, G=0, B=0]
var inputXy2 = ConvertRgbToXy(outputRgb); // xy [x=0.64, y=0.33]
}
private static RGBColor ConvertXyToRgb(xyChromaticity chromaticity)
{
var rgbColor = ConverterXyToRgb.Convert(chromaticity); // RGB [R=2.38, G=-10.5, B=-0.64]
// divide each channel by the largest value from R, G, B to have the maximum intensity
// smaller values than 0 are trucated to 0
var rgbChannelMaxValue = rgbColor.Vector.Max();
var rgbColorNormalized = new RGBColor(rgbColor.Vector.Select(c => c < 0 ? 0 : c / rgbChannelMaxValue).ToArray());
return rgbColorNormalized; // RGB [R=1, G=0, B=0]
}
private static xyChromaticity ConvertRgbToXy(RGBColor rgbColor)
{
var chromaticity = ConverterRgbToXy.Convert(rgbColor);
return chromaticity;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment