-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57d6611
commit 61b251c
Showing
33 changed files
with
876 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<LangVersion>8</LangVersion> | ||
<Nullable>disable</Nullable> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
|
||
<Authors>ParaParty</Authors> | ||
<Product>ParaParty Util Colors</Product> | ||
<PackageProjectUrl>https://pkg.para.party/ParaPartyUtil</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/ParaParty/ParaPartyUtil/</RepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<Description>ParaParty Util UnityNative</Description> | ||
|
||
<RootNamespace>Paraparty</RootNamespace> | ||
<AssemblyName>Paraparty.Colors</AssemblyName> | ||
|
||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<Version>3.0.0</Version> | ||
<AssemblyVersion>3.0.0.0</AssemblyVersion> | ||
<FileVersion>3.0.0.0</FileVersion> | ||
</PropertyGroup> | ||
|
||
</Project> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Text; | ||
#if !UNITY_2022_1_OR_NEWER | ||
using System.Drawing; | ||
#else | ||
using UnityEngine; | ||
#endif | ||
|
||
namespace Paraparty.Colors | ||
{ | ||
public static class ColorExtension | ||
{ | ||
public static string SerializeColor(this Color color) | ||
{ | ||
#if !UNITY_2022_1_OR_NEWER | ||
int r = color.R; | ||
int g = color.G; | ||
int b = color.B; | ||
int a = color.A; | ||
#else | ||
int r = (int)(color.r * 255); | ||
int g = (int)(color.g * 255); | ||
int b = (int)(color.b * 255); | ||
int a = (int)(color.a * 255); | ||
#endif | ||
|
||
var sb = new StringBuilder(); | ||
sb.Append("#"); | ||
|
||
sb.Append(r.ToString("X2")); | ||
sb.Append(g.ToString("X2")); | ||
sb.Append(b.ToString("X2")); | ||
if (a < 255) | ||
{ | ||
sb.Append(a.ToString("X2")); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
public static string SerializeColor(this Oklab color) | ||
{ | ||
return color.ToString(); | ||
} | ||
|
||
public static string SerializeColor(this Oklch color) | ||
{ | ||
return color.ToString(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#if !UNITY_2022_1_OR_NEWER | ||
using System.Drawing; | ||
#else | ||
using UnityEngine; | ||
#endif | ||
|
||
namespace Paraparty.Colors | ||
{ | ||
public static class ColorUtils | ||
{ | ||
public static string SerializeColor(Color color) | ||
{ | ||
var oklch = Oklch.FromColor(color); | ||
return oklch.ToString(); | ||
} | ||
|
||
|
||
public static Color ParseColor(string src) | ||
{ | ||
src = src.Trim(); | ||
if (string.IsNullOrWhiteSpace(src)) | ||
{ | ||
return Constance.White; | ||
} | ||
|
||
if (uint.TryParse(src, out var uintColor)) | ||
{ | ||
return ParseUintColor(uintColor); | ||
} | ||
|
||
if (src.StartsWith("#")) | ||
{ | ||
src = src[1..]; | ||
if (src.Length == 3 && | ||
Tools.TryParseHexToUint($"{src[0]}{src[0]}{src[1]}{src[1]}{src[2]}{src[2]}FF", out uintColor)) | ||
{ | ||
return ParseUintColor(uintColor); | ||
} | ||
|
||
if (src.Length == 6 && Tools.TryParseHexToUint($"{src}FF", out uintColor)) | ||
{ | ||
return ParseUintColor(uintColor); | ||
} | ||
|
||
if (src.Length == 8 && Tools.TryParseHexToUint($"{src}", out uintColor)) | ||
{ | ||
return ParseUintColor(uintColor); | ||
} | ||
} | ||
|
||
if (src.StartsWith("oklab(") && src.EndsWith(")")) | ||
{ | ||
return Oklab.ParseOklch(src).ToColor(); | ||
} | ||
|
||
if (src.StartsWith("oklch(") && src.EndsWith(")")) | ||
{ | ||
return Oklch.ParseOklch(src).ToColor(); | ||
} | ||
|
||
return Constance.White; | ||
} | ||
|
||
private static Color ParseUintColor(uint src) | ||
{ | ||
var b = new byte[] | ||
{ | ||
(byte)(src >> 24), | ||
(byte)((src >> 16) & 0x00FF), | ||
(byte)((src >> 8) & 0x0000FF), | ||
(byte)(src & 0x000000FF) | ||
}; | ||
return MakeColorFromRgba(b[0], b[1], b[2], b[3]); | ||
} | ||
|
||
internal static Color MakeColorFromRgba(int red, int green, int blue, int alpha) | ||
{ | ||
#if !UNITY_2022_1_OR_NEWER | ||
return Color.FromArgb(alpha, red, green, blue); | ||
#else | ||
return new Color(alpha * 1f / 255f, red * 1f / 255f, green * 1f / 255f, blue * 1f / 255f); | ||
#endif | ||
} | ||
|
||
internal static Color MakeColorFromRgbaF(float red, float green, float blue, float alpha) | ||
{ | ||
#if !UNITY_2022_1_OR_NEWER | ||
return Color.FromArgb( | ||
(int)(alpha * 255), | ||
(int)(red * 255), | ||
(int)(green * 255), | ||
(int)(blue * 255) | ||
); | ||
#else | ||
return new Color(alpha, red, green, blue); | ||
#endif | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#if !UNITY_2022_1_OR_NEWER | ||
using System.Drawing; | ||
#else | ||
using UnityEngine; | ||
#endif | ||
|
||
namespace Paraparty.Colors | ||
{ | ||
internal static class Constance | ||
{ | ||
internal static Color White = | ||
#if !UNITY_2022_1_OR_NEWER | ||
Color.White; | ||
#else | ||
Color.white; | ||
#endif | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.