├── .github └── workflows │ └── dotnet-core.yml ├── .gitignore ├── LICENSE ├── README.md ├── assets └── icon │ ├── ColorHashSharp-ORIGINAL.png │ └── ColorHashSharp-icon.png ├── samples ├── ColorHashSharp-Samples.sln └── ConsoleAppNetCore │ ├── ConsoleAppNetCore.csproj │ └── Program.cs └── src ├── ColorHashSharp.Tests ├── BKDRHashTest.cs ├── ColorHashSharp.Tests.csproj ├── ColorHashTest.cs ├── ColorToRgbTest.cs ├── OptionsTest.cs └── ReadmeCodeTest.cs ├── ColorHashSharp.sln └── ColorHashSharp ├── Assembly.cs ├── BKDRHash.cs ├── ColorHash.cs ├── ColorHashSharp.csproj ├── ColorToHex.cs ├── ColorToRgb.cs ├── Entities ├── Hsl.cs └── Hue.cs ├── Interfaces ├── IColorHash.cs └── IColorHashAlias.cs ├── Options.cs └── Utilities.cs /.github/workflows/dotnet-core.yml: -------------------------------------------------------------------------------- 1 | name: .NET Core 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: Setup .NET Core 17 | uses: actions/setup-dotnet@v1 18 | with: 19 | dotnet-version: 3.1.101 20 | - name: Install dependencies 21 | working-directory: src 22 | run: dotnet restore 23 | - name: Build 24 | working-directory: src 25 | run: dotnet build --configuration Release --no-restore 26 | - name: Test 27 | working-directory: src 28 | run: dotnet test --no-restore --verbosity normal 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jose A. Fernandez 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ColorHashSharp 2 | Generate color based on the given string. C# port of [ColorHash Javascript Library](https://github.com/zenozeng/color-hash). 3 | 4 | [![NuGet version (ColorHashSharp)](https://img.shields.io/nuget/v/ColorHashSharp.svg?style=flat-square)](https://www.nuget.org/packages/ColorHashSharp/) 5 | ![.NET Core](https://github.com/fernandezja/ColorHashSharp/workflows/.NET%20Core/badge.svg?branch=master) 6 | [![Build status](https://fernandezja.visualstudio.com/ColorHashSharp/_apis/build/status/ColorHashSharp-CI)](https://fernandezja.visualstudio.com/ColorHashSharp/_build/latest?definitionId=4) 7 | 8 | #### Basic 9 | 10 | ```csharp 11 | Debug.WriteLine($"ColorHash > Hello World"); 12 | 13 | var colorHash = new ColorHash(); 14 | 15 | //HSL (return HSL object) 16 | var hsl = colorHash.Hsl("Hello World"); 17 | 18 | Debug.WriteLine($" H = {hsl.H} | S = {hsl.S} | L = {hsl.L}"); 19 | // H = 225 | S = 0,35 | L = 0,65 20 | 21 | 22 | //RGB (return System.Drawing.Color object) 23 | var color = colorHash.Rgb("Hello World"); 24 | 25 | Debug.WriteLine($" R= {color.R} | G = {color.G} | B = {color.B}"); 26 | // R= 135 | G = 150 | B = 197 27 | 28 | //Hex (return string) 29 | var hex = colorHash.Hex("Hello World"); 30 | 31 | Debug.WriteLine($" hex = {hex}"); 32 | // hex = 8796C5 33 | ``` 34 | -------------------------------------------------------------------------------- /assets/icon/ColorHashSharp-ORIGINAL.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fernandezja/ColorHashSharp/cd940e3d1f8b1c3ce3587f7e13cc2e3779ac9dfa/assets/icon/ColorHashSharp-ORIGINAL.png -------------------------------------------------------------------------------- /assets/icon/ColorHashSharp-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fernandezja/ColorHashSharp/cd940e3d1f8b1c3ce3587f7e13cc2e3779ac9dfa/assets/icon/ColorHashSharp-icon.png -------------------------------------------------------------------------------- /samples/ColorHashSharp-Samples.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.28803.202 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleAppNetCore", "ConsoleAppNetCore\ConsoleAppNetCore.csproj", "{9E911093-4CB0-46FF-84BC-812A1ACAC37B}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {9E911093-4CB0-46FF-84BC-812A1ACAC37B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {9E911093-4CB0-46FF-84BC-812A1ACAC37B}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {9E911093-4CB0-46FF-84BC-812A1ACAC37B}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {9E911093-4CB0-46FF-84BC-812A1ACAC37B}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {5DC6A0F7-152A-4E82-8BC4-0E39B4AE5533} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /samples/ConsoleAppNetCore/ConsoleAppNetCore.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp3.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /samples/ConsoleAppNetCore/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using Fernandezja.ColorHashSharp; 4 | 5 | namespace ConsoleAppNetCore 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | Console.WriteLine("ColorHash > Hello World"); 12 | 13 | var colorHash = new ColorHash(); 14 | 15 | //HSL (return HSL object) 16 | var hsl = colorHash.Hsl("Hello World"); 17 | 18 | Console.WriteLine($" H = {hsl.H} | S = {hsl.S} | L = {hsl.L}"); 19 | Debug.WriteLine($" H = {hsl.H} | S = {hsl.S} | L = {hsl.L}"); 20 | 21 | 22 | //RGB (return System.Drawing.Color object) 23 | var color = colorHash.Rgb("Hello World"); 24 | 25 | Console.WriteLine($" R= {color.R} | G = {color.G} | B = {color.B}"); 26 | Debug.WriteLine($" R= {color.R} | G = {color.G} | B = {color.B}"); 27 | 28 | 29 | //Hex (return string) 30 | var hex = colorHash.Hex("Hello World"); 31 | 32 | Console.WriteLine($" hex = {hex}"); 33 | Debug.WriteLine($" hex = {hex}"); 34 | 35 | Console.WriteLine("Press any key to close..."); 36 | Console.ReadKey(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/ColorHashSharp.Tests/BKDRHashTest.cs: -------------------------------------------------------------------------------- 1 | using Fernandezja.ColorHashSharp; 2 | using System; 3 | using Xunit; 4 | 5 | namespace ColorHashSharp.Tests 6 | { 7 | public class BKDRHashTest 8 | { 9 | private const char SPACE_CHAR = ' '; 10 | 11 | [Fact(DisplayName = "Generate_HashWithYodaString")] 12 | public void GenerateHashWithYodaString() 13 | { 14 | var hash = new BKDRHash(); 15 | var result = hash.Generate("Yoda"); 16 | 17 | Assert.True(result > 0); 18 | Assert.Equal(26461759997, result); 19 | } 20 | 21 | [Fact(DisplayName = "Generate_HashWithHelloWorldString")] 22 | public void GenerateHashWithHelloWorldString() 23 | { 24 | var hash = new BKDRHash(); 25 | var result = hash.Generate("Hello World"); 26 | ulong expected = ulong.Parse("294020464607729"); 27 | 28 | Assert.True(result > 0); 29 | Assert.Equal(expected, result); 30 | } 31 | 32 | [Fact(DisplayName = "Generate_HashWithOneOnlyChar")] 33 | public void GenerateHashWithOneOnlyChar() 34 | { 35 | var hash = new BKDRHash(); 36 | var result = hash.Generate("y"); 37 | 38 | Assert.True(result > 0); 39 | Assert.Equal(15971, result); 40 | } 41 | 42 | [Theory(DisplayName = "Generate_HashWithSpaces")] 43 | [InlineData(1, 4312)] 44 | [InlineData(2, 553464)] 45 | [InlineData(100, 331584897437617)] 46 | [InlineData(250, 7527402707245979)] 47 | [InlineData(1000, 128829383441439)] 48 | public void Generate_HashWithSpaces(int spacesLength, ulong hashExpected) 49 | { 50 | var stringToHash = new string(SPACE_CHAR, spacesLength); 51 | 52 | var hash = new BKDRHash(); 53 | var result = hash.Generate(stringToHash); 54 | 55 | Assert.Equal(spacesLength, stringToHash.Length); 56 | Assert.True(result > 0); 57 | Assert.Equal(hashExpected, result); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/ColorHashSharp.Tests/ColorHashSharp.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.0 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | all 14 | runtime; build; native; contentfiles; analyzers 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/ColorHashSharp.Tests/ColorHashTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Text; 5 | using Fernandezja.ColorHashSharp; 6 | using Xunit; 7 | 8 | namespace ColorHashSharp.Tests 9 | { 10 | public class ColorHashTest 11 | { 12 | [Fact(DisplayName = "BuildToHsl_ShouldCreateAColorWithTextYodaInHSL")] 13 | public void BuildToHsl_ShouldCreateAColorWithHSL() 14 | { 15 | var colorHash = new Fernandezja.ColorHashSharp.ColorHash(); 16 | 17 | var result = colorHash.BuildToHsl("yoda"); 18 | 19 | Assert.NotNull(result); 20 | Assert.Equal(76, result.H); 21 | Assert.Equal(0.35, result.S); 22 | Assert.Equal(0.35, result.L); 23 | } 24 | 25 | 26 | 27 | [Theory(DisplayName = "BuildToHsl_ShouldCreateAColorInHSL")] 28 | [InlineData("yoda", 76, 0.35, 0.35)] 29 | [InlineData("Yoda", 314, 0.5, 0.65)] 30 | [InlineData("starwars", 339, 0.35, 0.5)] 31 | [InlineData("StarWars", 135, 0.5, 0.35)] 32 | public void BuildToHsl_ShouldCreateAColorInHSL( 33 | string phrase, double hExpected, double sExpected, double lExpected) 34 | { 35 | var colorHash = new Fernandezja.ColorHashSharp.ColorHash(); 36 | 37 | var result = colorHash.BuildToHsl(phrase); 38 | 39 | Assert.NotNull(result); 40 | Assert.Equal(hExpected, result.H); 41 | Assert.Equal(sExpected, result.S); 42 | Assert.Equal(lExpected, result.L); 43 | } 44 | 45 | [Theory(DisplayName = "BuildToHex_ShouldCreateAColorInHex")] 46 | [InlineData("yoda", "68783A")] 47 | [InlineData("Yoda", "D279BE")] 48 | [InlineData("starwars", "AC5372")] 49 | [InlineData("StarWars", "2D8643")] 50 | public void BuildToHex_ShouldCreateAColorInHex(string phrase, string hexExpected) 51 | { 52 | var colorHash = new Fernandezja.ColorHashSharp.ColorHash(); 53 | 54 | var result = colorHash.BuildToHex(phrase); 55 | 56 | Assert.NotNull(result); 57 | Assert.Equal(hexExpected, result); 58 | } 59 | 60 | [Fact(DisplayName = "BuildToColor_ShouldCreateAColorObject")] 61 | public void BuildToColor_ShouldCreateAColorObject() 62 | { 63 | var colorHash = new Fernandezja.ColorHashSharp.ColorHash(); 64 | 65 | var result = colorHash.BuildToColor("yoda"); 66 | 67 | Assert.Equal(Color.FromArgb(alpha:255,red:104,green:120, blue:58), 68 | result); 69 | } 70 | 71 | [Fact(DisplayName = "BuildToHsl_HelloWorldString")] 72 | public void BuildToHsl_HelloWorldString() 73 | { 74 | var colorHash = new Fernandezja.ColorHashSharp.ColorHash(); 75 | 76 | var result = colorHash.BuildToHsl("Hello World"); 77 | 78 | Assert.NotNull(result); 79 | Assert.Equal(225, result.H); 80 | Assert.Equal(0.35, result.S); 81 | Assert.Equal(0.65, result.L); 82 | } 83 | 84 | [Fact(DisplayName = "Build_HelloWorldStringToHslColorAndHex")] 85 | public void Build_HelloWorldStringToHslColorAndHex() 86 | { 87 | var ColorHash = new Fernandezja.ColorHashSharp.ColorHash(); 88 | 89 | var resultHsl = ColorHash.BuildToHsl("Hello World"); 90 | var resultColor = ColorHash.BuildToColor("Hello World"); 91 | var resultHex = ColorHash.BuildToHex("Hello World"); 92 | 93 | Assert.Equal(135, resultColor.R); 94 | Assert.Equal(150, resultColor.G); 95 | Assert.Equal(197, resultColor.B); 96 | 97 | Assert.Equal(225, resultHsl.H); 98 | Assert.Equal(0.35, resultHsl.S); 99 | Assert.Equal(0.65, resultHsl.L); 100 | 101 | Assert.Equal("8796C5", resultHex); 102 | 103 | 104 | } 105 | 106 | [Fact(DisplayName = "Hsl_ShouldCreateAColorInHSL")] 107 | public void Hsl_ShouldCreateAColorInHSL() 108 | { 109 | var colorHash = new Fernandezja.ColorHashSharp.ColorHash(); 110 | 111 | var result = colorHash.Hsl("Hello World"); 112 | 113 | Assert.NotNull(result); 114 | Assert.Equal(225, result.H); 115 | Assert.Equal(0.35, result.S); 116 | Assert.Equal(0.65, result.L); 117 | } 118 | 119 | 120 | 121 | [Fact(DisplayName = "Hex_ShouldCreateAColorInHex")] 122 | public void Hex_ShouldCreateAColorInHex() 123 | { 124 | var colorHash = new Fernandezja.ColorHashSharp.ColorHash(); 125 | 126 | var result = colorHash.BuildToHex("Hello World"); 127 | 128 | Assert.NotNull(result); 129 | Assert.Equal("8796C5", result); 130 | } 131 | 132 | 133 | [Fact(DisplayName = "Rgb_ShouldCreateAColorInRgb")] 134 | public void Rgb_ShouldCreateAColorInRgb() 135 | { 136 | var colorHash = new Fernandezja.ColorHashSharp.ColorHash(); 137 | 138 | var result = colorHash.Rgb("Hello World"); 139 | 140 | Assert.Equal(135, result.R); 141 | Assert.Equal(150, result.G); 142 | Assert.Equal(197, result.B); 143 | } 144 | 145 | 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/ColorHashSharp.Tests/ColorToRgbTest.cs: -------------------------------------------------------------------------------- 1 | using Fernandezja.ColorHashSharp; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using Xunit; 6 | 7 | namespace ColorHashSharp.Tests 8 | { 9 | public class ColorToRgbTest 10 | { 11 | 12 | [Fact(DisplayName = "ToRgb_HslToRgb")] 13 | public void ToRgb_HslToRgb() 14 | { 15 | //HSL from ""Hello World" 255, 0.35, 0.65 16 | //double hue, double saturation, double lightness 17 | var result = ColorToRgb.ToRgb(225, 0.35, 0.65); 18 | 19 | Assert.Equal(135, result.R); 20 | Assert.Equal(150, result.G); 21 | Assert.Equal(197, result.B); 22 | 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/ColorHashSharp.Tests/OptionsTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Drawing; 5 | using System.Text; 6 | using Fernandezja.ColorHashSharp; 7 | using Xunit; 8 | 9 | namespace ColorHashSharp.Tests 10 | { 11 | public class OptionsTest 12 | { 13 | 14 | [Fact(DisplayName = "GetLS_OneValue")] 15 | public void GetLS_OneValue() 16 | { 17 | var options = new Fernandezja.ColorHashSharp.Options(); 18 | var result = options.GetLS(0.35); 19 | Assert.NotNull(result); 20 | Assert.Single(result); 21 | Assert.Equal(0.35, result[0]); 22 | } 23 | 24 | [Fact(DisplayName = "GetLS_DefaultValues")] 25 | public void GetLS_DefaultValues() 26 | { 27 | var options = new Fernandezja.ColorHashSharp.Options(); 28 | var result = options.GetLS(); 29 | Assert.NotNull(result); 30 | Assert.Equal(3, result.Count); 31 | Assert.Equal(0.35, result[0]); 32 | Assert.Equal(0.5, result[1]); 33 | Assert.Equal(0.65, result[2]); 34 | } 35 | 36 | [Fact(DisplayName = "GetLS_ArrayValues")] 37 | public void GetLS_ArrayValues() 38 | { 39 | var options = new Fernandezja.ColorHashSharp.Options(); 40 | var result = options.GetLS(new ArrayList() { 0.35, 0.35, 0.35}); 41 | Assert.NotNull(result); 42 | Assert.Equal(3, result.Count); 43 | Assert.Equal(0.35, result[0]); 44 | Assert.Equal(0.35, result[1]); 45 | Assert.Equal(0.35, result[2]); 46 | } 47 | 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/ColorHashSharp.Tests/ReadmeCodeTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Xunit; 5 | using System.Diagnostics; 6 | using Fernandezja.ColorHashSharp; 7 | 8 | namespace ColorHashSharp.Tests 9 | { 10 | public class ReadmeCodeTest 11 | { 12 | [Fact(DisplayName = "ReadmeCodeExample_ToCopyPaste")] 13 | public void ReadmeCodeExample_ToCopyPaste() 14 | { 15 | Debug.WriteLine($"ColorHash > Hello World"); 16 | 17 | var colorHash = new ColorHash(); 18 | 19 | //HSL (return HSL object) 20 | var hsl = colorHash.Hsl("Hello World"); 21 | 22 | Debug.WriteLine($" H = {hsl.H} | S = {hsl.S} | L = {hsl.L}"); 23 | 24 | 25 | //RGB (return System.Drawing.Color object) 26 | var color = colorHash.Rgb("Hello World"); 27 | 28 | Debug.WriteLine($" R= {color.R} | G = {color.G} | B = {color.B}"); 29 | 30 | 31 | //Hex (return string) 32 | var hex = colorHash.Hex("Hello World"); 33 | 34 | Debug.WriteLine($" hex = {hex}"); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/ColorHashSharp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.168 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ColorHashSharp", "ColorHashSharp\ColorHashSharp.csproj", "{68766358-4CA1-4A57-94B9-0E1CF0F809BF}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ColorHashSharp.Tests", "ColorHashSharp.Tests\ColorHashSharp.Tests.csproj", "{2170724A-3466-4604-B243-D3B0936B44A3}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {68766358-4CA1-4A57-94B9-0E1CF0F809BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {68766358-4CA1-4A57-94B9-0E1CF0F809BF}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {68766358-4CA1-4A57-94B9-0E1CF0F809BF}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {68766358-4CA1-4A57-94B9-0E1CF0F809BF}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {2170724A-3466-4604-B243-D3B0936B44A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {2170724A-3466-4604-B243-D3B0936B44A3}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {2170724A-3466-4604-B243-D3B0936B44A3}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {2170724A-3466-4604-B243-D3B0936B44A3}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {627C67CF-7341-4A16-BF8A-8EF505E6F60B} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /src/ColorHashSharp/Assembly.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | [assembly: InternalsVisibleTo("ColorHashSharp.Tests")] 4 | -------------------------------------------------------------------------------- /src/ColorHashSharp/BKDRHash.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Numerics; 4 | using System.Text; 5 | 6 | namespace Fernandezja.ColorHashSharp 7 | { 8 | public class BKDRHash 9 | { 10 | private const char PADDING_CHAR = 'x'; 11 | 12 | /// 13 | /// The Number.MAX_SAFE_INTEGER constant represents the maximum safe integer in JavaScript 14 | /// https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Number/MAX_SAFE_INTEGER 15 | /// 16 | private const ulong JAVASCRIPT_MAX_SAFE_INTEGER = 9007199254740991; 17 | 18 | private const ulong SEED = 131; 19 | 20 | private const ulong SEED2 = 137; 21 | 22 | 23 | public ulong Generate(string value) { 24 | return GenerateVersion3(value); 25 | } 26 | 27 | 28 | 29 | public ulong GenerateVersion2(string value) 30 | { 31 | long hash = 0; 32 | 33 | // Make hash more sensitive for short string like 'a', 'b', 'c' 34 | var valueWithPadding = $"{value}{PADDING_CHAR}"; 35 | 36 | var valueUtf8 = ToUTF8(valueWithPadding); 37 | 38 | var max = (Int64.MaxValue / (long)SEED); 39 | 40 | for (int i = 0; i < valueUtf8.Length; i++) 41 | { 42 | if (hash > max) 43 | { 44 | Debug.WriteLine($"hash > max"); 45 | Debug.WriteLine($" hash = {hash}"); 46 | Debug.WriteLine($" max = {max} >> ToUInt64(max) = {Convert.ToUInt64(max)}"); 47 | Debug.WriteLine($" seed2 = {SEED2}"); 48 | 49 | hash = (hash / (long)SEED2); 50 | 51 | Debug.WriteLine($" new hash = {hash}"); 52 | Debug.WriteLine($" "); 53 | } 54 | 55 | var bytes = ToUTF8Bytes(valueUtf8[i].ToString()); 56 | 57 | Debug.WriteLine($"{valueUtf8[i].ToString()} > {bytes[0]}"); 58 | 59 | hash = (hash * (long)SEED) + bytes[0]; 60 | 61 | Debug.WriteLine($"{valueUtf8[i].ToString()} > {bytes[0]} > hash = {hash}"); 62 | } 63 | 64 | return (ulong)hash; 65 | } 66 | 67 | 68 | 69 | /// 70 | /// BKDR Hash (modified version). Idem original code. 71 | /// https://github.com/zenozeng/color-hash/blob/master/lib/bkdr-hash.js 72 | /// Example values nodejs https://repl.it/@Jose_AA/BKDR-Hash 73 | /// 74 | /// 75 | /// 76 | public ulong GenerateVersion3(string value) 77 | { 78 | ulong hash = 0; 79 | 80 | // Make hash more sensitive for short string like 'a', 'b', 'c' 81 | var valueWithPadding = $"{value}{PADDING_CHAR}"; 82 | 83 | var valueUtf8 = ToUTF8(valueWithPadding); 84 | 85 | var max = (JAVASCRIPT_MAX_SAFE_INTEGER / SEED); 86 | 87 | for (int i = 0; i < valueUtf8.Length; i++) 88 | { 89 | if (hash > max) 90 | { 91 | Debug.WriteLine($"hash > max"); 92 | Debug.WriteLine($" hash = {hash}"); 93 | Debug.WriteLine($" max = {max} >> ToUInt64(max) = {Convert.ToUInt64(max)}"); 94 | Debug.WriteLine($" seed2 = {SEED2}"); 95 | 96 | hash = (hash / SEED2); 97 | 98 | Debug.WriteLine($" new hash = {hash}"); 99 | Debug.WriteLine($" "); 100 | } 101 | 102 | var bytes = ToUTF8Bytes(valueUtf8[i].ToString()); 103 | 104 | Debug.WriteLine($"{valueUtf8[i].ToString()} > {bytes[0]}"); 105 | 106 | hash = (hash * SEED) + bytes[0]; 107 | 108 | Debug.WriteLine($"{valueUtf8[i].ToString()} > {bytes[0]} > hash = {hash}"); 109 | } 110 | 111 | return hash; 112 | } 113 | 114 | private string ToUTF8(string value) 115 | { 116 | var bytes = Encoding.Default.GetBytes(value); 117 | return Encoding.UTF8.GetString(bytes); 118 | } 119 | 120 | private byte[] ToUTF8Bytes(string value) 121 | { 122 | var bytes = Encoding.UTF8.GetBytes(value); 123 | return bytes; 124 | } 125 | 126 | 127 | 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/ColorHashSharp/ColorHash.cs: -------------------------------------------------------------------------------- 1 | using Fernandezja.ColorHashSharp.Entities; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Collections; 5 | using System.Text; 6 | using System.Drawing; 7 | using Fernandezja.ColorHashSharp.Interfaces; 8 | 9 | namespace Fernandezja.ColorHashSharp 10 | { 11 | public class ColorHash : IColorHash, IColorHashAlias 12 | { 13 | 14 | private Options _options; 15 | 16 | public ColorHash() 17 | { 18 | _options = new Options(); 19 | } 20 | 21 | #region IColorHash 22 | 23 | public string Build(string value) 24 | { 25 | return BuildToHex(value); 26 | } 27 | 28 | public Color BuildToColor(string value) 29 | { 30 | var hsl = BuildToHsl(value); 31 | var color = (new ColorToRgb()).ToRgb(hsl); 32 | return color; 33 | } 34 | 35 | public string BuildToHex(string value) 36 | { 37 | var color = BuildToColor(value); 38 | var hex = (new ColorToHex()).BuildToHex(color); 39 | return hex; 40 | } 41 | 42 | public Hsl BuildToHsl(string value) 43 | { 44 | double h, s, l; 45 | 46 | var hashGenerator = new BKDRHash(); 47 | 48 | var hash = hashGenerator.Generate(value); 49 | 50 | if (_options.HueRanges.Count > 0) 51 | { 52 | var rangeIndex = hash % Convert.ToUInt64(_options.HueRanges.Count); 53 | 54 | //TODO: Convert int? prevent error 55 | var hueValue = (Hue)_options.HueRanges[(int)rangeIndex]; 56 | 57 | var hueResolution = Convert.ToUInt64(727); 58 | h = ((hash / Convert.ToUInt64(_options.HueRanges.Count)) % hueResolution) 59 | * (Convert.ToUInt64(hueValue.Max) - Convert.ToUInt64(hueValue.Min)) / hueResolution + Convert.ToUInt64(hueValue.Min); 60 | 61 | } 62 | else 63 | { 64 | h = hash % 359; // note that 359 is a prime 65 | } 66 | 67 | hash = (hash / 360); 68 | var sIndex = (hash % (ulong)_options.S.Count); 69 | s = (double)_options.S[(int)sIndex]; 70 | 71 | hash = (hash / (ulong)_options.S.Count); 72 | var lIndex = (hash % (ulong)_options.L.Count); 73 | l = (double)_options.L[(int)lIndex]; 74 | 75 | var hslResult = new Hsl(h, s, l); 76 | 77 | return hslResult; 78 | } 79 | #endregion 80 | 81 | #region IColorHashAlias 82 | 83 | /// 84 | /// Alias of BuildToColor method 85 | /// 86 | /// 87 | /// 88 | public Color Rgb(string value) { 89 | return BuildToColor(value); 90 | } 91 | 92 | /// 93 | /// Alias of BuildToHex method 94 | /// 95 | /// 96 | /// 97 | public string Hex(string value) 98 | { 99 | return BuildToHex(value); 100 | } 101 | 102 | 103 | /// 104 | /// Alias of BuildToHsl method 105 | /// 106 | /// 107 | /// 108 | public Hsl Hsl(string value) 109 | { 110 | return BuildToHsl(value); 111 | } 112 | 113 | #endregion 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/ColorHashSharp/ColorHashSharp.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.1 5 | Fernandezja.ColorHashSharp 6 | false 7 | 1.0.0.0 8 | 1.0.0 9 | 1.0.0 10 | Generate color based on the given string 11 | Generate color based on the given string. C# port of ColorHash Javascript Library. 12 | Jose A. Fernandez 13 | https://licenses.nuget.org/MIT 14 | 15 | https://raw.githubusercontent.com/fernandezja/ColorHashSharp/master/assets/icon/ColorHashSharp-icon.png 16 | https://github.com/fernandezja/ColorHashSharp 17 | color hash string hsl hex hexadecimal 18 | Generate color based on the given string. C# port of ColorHash Javascript Library. See https://github.com/fernandezja/ColorHashSharp 19 | true 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/ColorHashSharp/ColorToHex.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Text; 5 | 6 | namespace Fernandezja.ColorHashSharp 7 | { 8 | public class ColorToHex 9 | { 10 | public string BuildToHex(Color color) { 11 | string hex = color.R.ToString("X2") 12 | + color.G.ToString("X2") 13 | + color.B.ToString("X2"); 14 | return hex; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/ColorHashSharp/ColorToRgb.cs: -------------------------------------------------------------------------------- 1 | using Fernandezja.ColorHashSharp.Entities; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Diagnostics; 5 | using System.Drawing; 6 | using System.Text; 7 | 8 | namespace Fernandezja.ColorHashSharp 9 | { 10 | public class ColorToRgb 11 | { 12 | public Color ToRgb(Hsl hsl) { 13 | return ToRgb(hsl.H, hsl.S, hsl.L); 14 | } 15 | 16 | 17 | public static Color ToRgb(double hue, double saturation, double lightness) { 18 | return ToRgb2(hue, saturation, lightness); 19 | } 20 | 21 | 22 | 23 | /// 24 | /// From https://en.wikipedia.org/wiki/HSL_and_HSV 25 | /// 26 | /// 27 | /// 28 | /// 29 | /// 30 | public static Color ToRgb1(double hue, double saturation, double lightness) 31 | { 32 | int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6; 33 | double f = hue / 60 - Math.Floor(hue / 60); 34 | 35 | lightness = lightness * 255; 36 | int v = Convert.ToInt32(lightness); 37 | int p = Convert.ToInt32(lightness * (1 - saturation)); 38 | int q = Convert.ToInt32(lightness * (1 - f * saturation)); 39 | int t = Convert.ToInt32(lightness * (1 - (1 - f) * saturation)); 40 | 41 | if (hi == 0) 42 | return Color.FromArgb(255, v, t, p); 43 | else if (hi == 1) 44 | return Color.FromArgb(255, q, v, p); 45 | else if (hi == 2) 46 | return Color.FromArgb(255, p, v, t); 47 | else if (hi == 3) 48 | return Color.FromArgb(255, p, q, v); 49 | else if (hi == 4) 50 | return Color.FromArgb(255, t, p, v); 51 | else 52 | return Color.FromArgb(255, v, p, q); 53 | } 54 | 55 | 56 | 57 | /// 58 | /// Idem HSL2RGB from zenozeng/color-hash 59 | /// https://github.com/zenozeng/color-hash/blob/master/dist/color-hash.js 60 | /// 61 | /// 62 | /// 63 | /// 64 | /// 65 | public static Color ToRgb2(double hue, double saturation, double lightness) 66 | { 67 | var h = hue / 360; 68 | Debug.WriteLine($"h = {h}"); 69 | 70 | var q = lightness < 0.5 71 | ? lightness * (1 + saturation) 72 | : lightness + saturation - (lightness * saturation); 73 | 74 | 75 | var p = 2.0 * lightness - q; 76 | 77 | Debug.WriteLine($"q = {q}"); 78 | Debug.WriteLine($"p = {p}"); 79 | 80 | var r = GetColor(h + 1 / 3.0, q, p); 81 | var g = GetColor(h, q, p); 82 | var b = GetColor(h - 1 / 3.0, q, p); 83 | 84 | return Color.FromArgb(alpha: 255, red: r, green: g, blue: b); 85 | 86 | 87 | } 88 | 89 | private static int GetColor(double color, double q, double p) { 90 | 91 | Debug.WriteLine($" 1 color = {color}"); 92 | 93 | if (color < 0) 94 | { 95 | color++; 96 | } 97 | 98 | if (color > 1) 99 | { 100 | color--; 101 | } 102 | 103 | if (color < (1.0/6.0)) 104 | { 105 | color = p + (q - p) * 6.0 * color; 106 | } 107 | else if (color < 0.5) 108 | { 109 | color = q; 110 | } 111 | else if (color < (2.0/3.0)) 112 | { 113 | color = p + (q - p) * 6.0 * ((2.0 / 3.0) - color); 114 | } 115 | else 116 | { 117 | color = p; 118 | } 119 | 120 | Debug.WriteLine($" 2 color = {color}"); 121 | 122 | Debug.WriteLine($" ----------------------------"); 123 | 124 | return (int)Math.Round(color * 255); 125 | 126 | 127 | } 128 | 129 | 130 | 131 | //public Color ToRgb(double h, double s, double l) 132 | //{ 133 | // double r = 0, 134 | // g = 0, 135 | // b = 0; 136 | 137 | // if (l != 0) 138 | // { 139 | // if (s == 0) 140 | // r = g = b = l; 141 | // else 142 | // { 143 | // double temp2; 144 | // if (l < 0.5) 145 | // temp2 = l * (1.0 + s); 146 | // else 147 | // temp2 = l + s - (l * s); 148 | 149 | // double temp1 = 2.0 * l - temp2; 150 | 151 | // r = GetColorComponent(temp1, temp2, h + 1.0 / 3.0); 152 | // g = GetColorComponent(temp1, temp2, h); 153 | // b = GetColorComponent(temp1, temp2, h - 1.0 / 3.0); 154 | // } 155 | // } 156 | // return Color.FromArgb((int)(255 * r), (int)(255 * g), (int)(255 * b)); 157 | 158 | //} 159 | 160 | //private static double GetColorComponent( 161 | // double temp1, double temp2, double temp3) 162 | //{ 163 | // if (temp3 < 0.0) 164 | // temp3 += 1.0; 165 | // else if (temp3 > 1.0) 166 | // temp3 -= 1.0; 167 | 168 | // if (temp3 < 1.0 / 6.0) 169 | // return temp1 + (temp2 - temp1) * 6.0 * temp3; 170 | // else if (temp3 < 0.5) 171 | // return temp2; 172 | // else if (temp3 < 2.0 / 3.0) 173 | // return temp1 + ((temp2 - temp1) * ((2.0 / 3.0) - temp3) * 6.0); 174 | // else 175 | // return temp1; 176 | //} 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /src/ColorHashSharp/Entities/Hsl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Fernandezja.ColorHashSharp.Entities 6 | { 7 | /// 8 | /// HSL (hue, saturation, lightness) 9 | /// SL (hue, saturation, lightness) and HSV (hue, saturation, value) 10 | /// are alternative representations of the RGB color model 11 | /// https://en.wikipedia.org/wiki/HSL_and_HSV 12 | /// 13 | [Serializable] 14 | public class Hsl 15 | { 16 | public Hsl() 17 | { 18 | 19 | } 20 | 21 | public Hsl(double h, double s, double l) 22 | { 23 | H = h; 24 | S = s; 25 | L = l; 26 | } 27 | 28 | 29 | public double H { get; set; } 30 | public double S { get; set; } 31 | public double L { get; set; } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/ColorHashSharp/Entities/Hue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Fernandezja.ColorHashSharp.Entities 6 | { 7 | /// 8 | /// Hue is one of the main properties (called color appearance parameters) of a color. 9 | /// Defined technically (in the CIECAM02 model) 10 | /// https://en.wikipedia.org/wiki/Hue 11 | /// 12 | [Serializable] 13 | public class Hue 14 | { 15 | public Hue() 16 | { 17 | Min = 0; 18 | Max = 360; 19 | } 20 | 21 | public int Min { get; set; } 22 | public int Max { get; set; } 23 | 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/ColorHashSharp/Interfaces/IColorHash.cs: -------------------------------------------------------------------------------- 1 | using System.Drawing; 2 | using Fernandezja.ColorHashSharp.Entities; 3 | 4 | namespace Fernandezja.ColorHashSharp.Interfaces 5 | { 6 | public interface IColorHash 7 | { 8 | string Build(string value); 9 | Color BuildToColor(string value); 10 | string BuildToHex(string value); 11 | Hsl BuildToHsl(string value); 12 | } 13 | } -------------------------------------------------------------------------------- /src/ColorHashSharp/Interfaces/IColorHashAlias.cs: -------------------------------------------------------------------------------- 1 | using System.Drawing; 2 | using Fernandezja.ColorHashSharp.Entities; 3 | 4 | namespace Fernandezja.ColorHashSharp.Interfaces 5 | { 6 | public interface IColorHashAlias { 7 | 8 | /// 9 | /// Alias of BuildToColor method 10 | /// 11 | /// 12 | /// 13 | Color Rgb(string value); 14 | 15 | /// 16 | /// Alias of BuildToHex method 17 | /// 18 | /// 19 | /// 20 | string Hex(string value); 21 | 22 | 23 | /// 24 | /// Alias of BuildToHsl method 25 | /// 26 | /// 27 | /// 28 | Hsl Hsl(string value); 29 | } 30 | } -------------------------------------------------------------------------------- /src/ColorHashSharp/Options.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Fernandezja.ColorHashSharp 7 | { 8 | public class Options 9 | { 10 | public ArrayList HueRanges { get; set; } 11 | 12 | /// 13 | /// Saturation 14 | /// 15 | public ArrayList S { get; set; } 16 | 17 | /// 18 | /// Lightness 19 | /// 20 | public ArrayList L { get; set; } 21 | 22 | public Options() 23 | { 24 | //TODO: Get from options param 25 | S = GetLS(new ArrayList() { 0.35, 0.5, 0.65 }); 26 | L = GetLS(new ArrayList() { 0.35, 0.5, 0.65 }); 27 | 28 | HueRanges = new ArrayList(); 29 | } 30 | 31 | 32 | /// 33 | /// Get lightness or saturation 34 | /// 35 | /// 36 | /// 37 | internal protected ArrayList GetLS(double param) 38 | { 39 | return new ArrayList() { param }; 40 | } 41 | 42 | /// 43 | /// Get lightness or saturation 44 | /// 45 | /// 46 | /// 47 | internal protected ArrayList GetLS(ArrayList param) 48 | { 49 | if (param == null) 50 | { 51 | // note that 3 is a prime 52 | param = new ArrayList() { 0.35, 0.5, 0.65 }; 53 | } 54 | 55 | return param; 56 | } 57 | 58 | internal protected ArrayList GetLS() 59 | { 60 | return GetLS(null); 61 | } 62 | 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/ColorHashSharp/Utilities.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Fernandezja.ColorHashSharp 6 | { 7 | public class Utilities 8 | { 9 | internal protected string StringToNumber(string value) 10 | { 11 | var result = new StringBuilder(); 12 | for (var i = 0; i < value.Length; i++) 13 | { 14 | result.Append(((int)value[i]).ToString()); 15 | } 16 | return result.ToString(); 17 | } 18 | 19 | } 20 | } 21 | --------------------------------------------------------------------------------