├── .gitignore ├── Common ├── ColorScheme.cs ├── Luminosity.cs ├── Options.cs ├── RandomColor.cs └── Range.cs ├── LICENSE ├── README.md ├── RandomColor.sln ├── RandomColorDemoScreenshot.png ├── RandomColorGenerator.Forms └── RandomColorGenerator.Forms.csproj ├── RandomColorGenerator.NetStandard └── RandomColorGenerator.NetStandard.csproj ├── RandomColorGenerator.WPF └── RandomColorGenerator.WPF.csproj ├── RandomColorSample ├── App.xaml ├── App.xaml.cs ├── MainWindow.xaml ├── MainWindow.xaml.cs ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings └── RandomColorSample.csproj └── packaging ├── Build RandomColorGenerator.Forms.bat ├── Build RandomColorGenerator.NetStandard.bat └── Build RandomColorGenerator.WPF.bat /.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 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | 33 | # Visual Studio 2015/2017 cache/options directory 34 | .vs/ 35 | # Uncomment if you have tasks that create the project's static files in wwwroot 36 | #wwwroot/ 37 | 38 | # Visual Studio 2017 auto generated files 39 | Generated\ Files/ 40 | 41 | # MSTest test Results 42 | [Tt]est[Rr]esult*/ 43 | [Bb]uild[Ll]og.* 44 | 45 | # NUnit 46 | *.VisualState.xml 47 | TestResult.xml 48 | nunit-*.xml 49 | 50 | # Build Results of an ATL Project 51 | [Dd]ebugPS/ 52 | [Rr]eleasePS/ 53 | dlldata.c 54 | 55 | # Benchmark Results 56 | BenchmarkDotNet.Artifacts/ 57 | 58 | # .NET Core 59 | project.lock.json 60 | project.fragment.lock.json 61 | artifacts/ 62 | 63 | # StyleCop 64 | StyleCopReport.xml 65 | 66 | # Files built by Visual Studio 67 | *_i.c 68 | *_p.c 69 | *_h.h 70 | *.ilk 71 | *.meta 72 | *.obj 73 | *.iobj 74 | *.pch 75 | *.pdb 76 | *.ipdb 77 | *.pgc 78 | *.pgd 79 | *.rsp 80 | *.sbr 81 | *.tlb 82 | *.tli 83 | *.tlh 84 | *.tmp 85 | *.tmp_proj 86 | *_wpftmp.csproj 87 | *.log 88 | *.vspscc 89 | *.vssscc 90 | .builds 91 | *.pidb 92 | *.svclog 93 | *.scc 94 | 95 | # Chutzpah Test files 96 | _Chutzpah* 97 | 98 | # Visual C++ cache files 99 | ipch/ 100 | *.aps 101 | *.ncb 102 | *.opendb 103 | *.opensdf 104 | *.sdf 105 | *.cachefile 106 | *.VC.db 107 | *.VC.VC.opendb 108 | 109 | # Visual Studio profiler 110 | *.psess 111 | *.vsp 112 | *.vspx 113 | *.sap 114 | 115 | # Visual Studio Trace Files 116 | *.e2e 117 | 118 | # TFS 2012 Local Workspace 119 | $tf/ 120 | 121 | # Guidance Automation Toolkit 122 | *.gpState 123 | 124 | # ReSharper is a .NET coding add-in 125 | _ReSharper*/ 126 | *.[Rr]e[Ss]harper 127 | *.DotSettings.user 128 | 129 | # JustCode is a .NET coding add-in 130 | .JustCode 131 | 132 | # TeamCity is a build add-in 133 | _TeamCity* 134 | 135 | # DotCover is a Code Coverage Tool 136 | *.dotCover 137 | 138 | # AxoCover is a Code Coverage Tool 139 | .axoCover/* 140 | !.axoCover/settings.json 141 | 142 | # Visual Studio code coverage results 143 | *.coverage 144 | *.coveragexml 145 | 146 | # NCrunch 147 | _NCrunch_* 148 | .*crunch*.local.xml 149 | nCrunchTemp_* 150 | 151 | # MightyMoose 152 | *.mm.* 153 | AutoTest.Net/ 154 | 155 | # Web workbench (sass) 156 | .sass-cache/ 157 | 158 | # Installshield output folder 159 | [Ee]xpress/ 160 | 161 | # DocProject is a documentation generator add-in 162 | DocProject/buildhelp/ 163 | DocProject/Help/*.HxT 164 | DocProject/Help/*.HxC 165 | DocProject/Help/*.hhc 166 | DocProject/Help/*.hhk 167 | DocProject/Help/*.hhp 168 | DocProject/Help/Html2 169 | DocProject/Help/html 170 | 171 | # Click-Once directory 172 | publish/ 173 | 174 | # Publish Web Output 175 | *.[Pp]ublish.xml 176 | *.azurePubxml 177 | # Note: Comment the next line if you want to checkin your web deploy settings, 178 | # but database connection strings (with potential passwords) will be unencrypted 179 | *.pubxml 180 | *.publishproj 181 | 182 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 183 | # checkin your Azure Web App publish settings, but sensitive information contained 184 | # in these scripts will be unencrypted 185 | PublishScripts/ 186 | 187 | # NuGet Packages 188 | *.nupkg 189 | # NuGet Symbol Packages 190 | *.snupkg 191 | # The packages folder can be ignored because of Package Restore 192 | **/[Pp]ackages/* 193 | # except build/, which is used as an MSBuild target. 194 | !**/[Pp]ackages/build/ 195 | # Uncomment if necessary however generally it will be regenerated when needed 196 | #!**/[Pp]ackages/repositories.config 197 | # NuGet v3's project.json files produces more ignorable files 198 | *.nuget.props 199 | *.nuget.targets 200 | 201 | # Microsoft Azure Build Output 202 | csx/ 203 | *.build.csdef 204 | 205 | # Microsoft Azure Emulator 206 | ecf/ 207 | rcf/ 208 | 209 | # Windows Store app package directories and files 210 | AppPackages/ 211 | BundleArtifacts/ 212 | Package.StoreAssociation.xml 213 | _pkginfo.txt 214 | *.appx 215 | *.appxbundle 216 | *.appxupload 217 | 218 | # Visual Studio cache files 219 | # files ending in .cache can be ignored 220 | *.[Cc]ache 221 | # but keep track of directories ending in .cache 222 | !?*.[Cc]ache/ 223 | 224 | # Others 225 | ClientBin/ 226 | ~$* 227 | *~ 228 | *.dbmdl 229 | *.dbproj.schemaview 230 | *.jfm 231 | *.pfx 232 | *.publishsettings 233 | orleans.codegen.cs 234 | 235 | # Including strong name files can present a security risk 236 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 237 | #*.snk 238 | 239 | # Since there are multiple workflows, uncomment next line to ignore bower_components 240 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 241 | #bower_components/ 242 | 243 | # RIA/Silverlight projects 244 | Generated_Code/ 245 | 246 | # Backup & report files from converting an old project file 247 | # to a newer Visual Studio version. Backup files are not needed, 248 | # because we have git ;-) 249 | _UpgradeReport_Files/ 250 | Backup*/ 251 | UpgradeLog*.XML 252 | UpgradeLog*.htm 253 | ServiceFabricBackup/ 254 | *.rptproj.bak 255 | 256 | # SQL Server files 257 | *.mdf 258 | *.ldf 259 | *.ndf 260 | 261 | # Business Intelligence projects 262 | *.rdl.data 263 | *.bim.layout 264 | *.bim_*.settings 265 | *.rptproj.rsuser 266 | *- [Bb]ackup.rdl 267 | *- [Bb]ackup ([0-9]).rdl 268 | *- [Bb]ackup ([0-9][0-9]).rdl 269 | 270 | # Microsoft Fakes 271 | FakesAssemblies/ 272 | 273 | # GhostDoc plugin setting file 274 | *.GhostDoc.xml 275 | 276 | # Node.js Tools for Visual Studio 277 | .ntvs_analysis.dat 278 | node_modules/ 279 | 280 | # Visual Studio 6 build log 281 | *.plg 282 | 283 | # Visual Studio 6 workspace options file 284 | *.opt 285 | 286 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 287 | *.vbw 288 | 289 | # Visual Studio LightSwitch build output 290 | **/*.HTMLClient/GeneratedArtifacts 291 | **/*.DesktopClient/GeneratedArtifacts 292 | **/*.DesktopClient/ModelManifest.xml 293 | **/*.Server/GeneratedArtifacts 294 | **/*.Server/ModelManifest.xml 295 | _Pvt_Extensions 296 | 297 | # Paket dependency manager 298 | .paket/paket.exe 299 | paket-files/ 300 | 301 | # FAKE - F# Make 302 | .fake/ 303 | 304 | # CodeRush personal settings 305 | .cr/personal 306 | 307 | # Python Tools for Visual Studio (PTVS) 308 | __pycache__/ 309 | *.pyc 310 | 311 | # Cake - Uncomment if you are using it 312 | # tools/** 313 | # !tools/packages.config 314 | 315 | # Tabs Studio 316 | *.tss 317 | 318 | # Telerik's JustMock configuration file 319 | *.jmconfig 320 | 321 | # BizTalk build output 322 | *.btp.cs 323 | *.btm.cs 324 | *.odx.cs 325 | *.xsd.cs 326 | 327 | # OpenCover UI analysis results 328 | OpenCover/ 329 | 330 | # Azure Stream Analytics local run output 331 | ASALocalRun/ 332 | 333 | # MSBuild Binary and Structured Log 334 | *.binlog 335 | 336 | # NVidia Nsight GPU debugger configuration file 337 | *.nvuser 338 | 339 | # MFractors (Xamarin productivity tool) working folder 340 | .mfractor/ 341 | 342 | # Local History for Visual Studio 343 | .localhistory/ 344 | 345 | # BeatPulse healthcheck temp database 346 | healthchecksdb 347 | 348 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 349 | MigrationBackup/ 350 | -------------------------------------------------------------------------------- /Common/ColorScheme.cs: -------------------------------------------------------------------------------- 1 | namespace RandomColorGenerator 2 | { 3 | /// 4 | /// Predefined color schemes. 5 | /// 6 | public enum ColorScheme 7 | { 8 | /// 9 | /// Select randomly from among the other color schemes. 10 | /// 11 | Random, 12 | /// 13 | /// Generates only grayscale colors. 14 | /// 15 | Monochrome, 16 | /// 17 | /// Generates only red colors. 18 | /// 19 | Red, 20 | /// 21 | /// Generates only orange colors. 22 | /// 23 | Orange, 24 | /// 25 | /// Generates only yellow colors. 26 | /// 27 | Yellow, 28 | /// 29 | /// Generates only green colors. 30 | /// 31 | Green, 32 | /// 33 | /// Generates only blue colors. 34 | /// 35 | Blue, 36 | /// 37 | /// Generates only purple colors. 38 | /// 39 | Purple, 40 | /// 41 | /// Generates only pink colors. 42 | /// 43 | Pink 44 | } 45 | } -------------------------------------------------------------------------------- /Common/Luminosity.cs: -------------------------------------------------------------------------------- 1 | namespace RandomColorGenerator 2 | { 3 | /// 4 | /// Predefined luminosity ranges. 5 | /// 6 | public enum Luminosity 7 | { 8 | /// 9 | /// Select randomly from among the other luminosities. 10 | /// 11 | Random, 12 | /// 13 | /// Generate dark colors. 14 | /// 15 | Dark, 16 | /// 17 | /// Generate light, pastel colors. 18 | /// 19 | Light, 20 | /// 21 | /// Generate vibrant colors. 22 | /// 23 | Bright, 24 | } 25 | } -------------------------------------------------------------------------------- /Common/Options.cs: -------------------------------------------------------------------------------- 1 | namespace RandomColorGenerator 2 | { 3 | /// 4 | /// Options for generating a random color. 5 | /// 6 | public class Options 7 | { 8 | /// 9 | /// Gets or sets the color scheme to use when generating the color. 10 | /// 11 | public ColorScheme ColorScheme { get; set; } 12 | /// 13 | /// Gets or sets the luminosity range to use when generating the color. 14 | /// 15 | public Luminosity Luminosity { get; set; } 16 | 17 | /// 18 | /// Creates a new instance using default values. 19 | /// 20 | public Options() 21 | {} 22 | 23 | /// 24 | /// Creates a new instance with the given color scheme and luminosity range. 25 | /// 26 | /// The color scheme to use when generating the color. 27 | /// The luminosity range to use when generating the color. 28 | public Options(ColorScheme scheme, Luminosity luminosity) 29 | { 30 | ColorScheme = scheme; 31 | Luminosity = luminosity; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /Common/RandomColor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.Linq; 5 | 6 | #if WPF 7 | using Point = System.Windows.Point; 8 | using Color = System.Windows.Media.Color; 9 | #elif NETSTANDARD || FORMS 10 | using Point = System.Drawing.Point; 11 | using Color = System.Drawing.Color; 12 | #else 13 | ERROR 14 | #endif 15 | 16 | namespace RandomColorGenerator 17 | { 18 | /// 19 | /// Generates random numbers. 20 | /// 21 | public static class RandomColor 22 | { 23 | private class DefinedColor 24 | { 25 | public Range HueRange { get; set; } 26 | public Point[] LowerBounds { get; set; } 27 | public Range SaturationRange { get; set; } 28 | public Range BrightnessRange { get; set; } 29 | } 30 | 31 | private static readonly Dictionary ColorDictionary = new Dictionary(); 32 | private static readonly object LockObj = new object(); 33 | private static Random _rng = new Random(); 34 | 35 | static RandomColor() 36 | { 37 | // Populate the color dictionary 38 | LoadColorBounds(); 39 | } 40 | 41 | /// 42 | /// Gets a new random color. 43 | /// 44 | /// Which color schemed to use when generating the color. 45 | /// The desired luminosity of the color. 46 | public static Color GetColor(ColorScheme scheme, Luminosity luminosity) 47 | { 48 | int H, S, B; 49 | 50 | // First we pick a hue (H) 51 | H = PickHue(scheme); 52 | 53 | // Then use H to determine saturation (S) 54 | S = PickSaturation(H, luminosity, scheme); 55 | 56 | // Then use S and H to determine brightness (B). 57 | B = PickBrightness(H, S, luminosity); 58 | 59 | // Then we return the HSB color in the desired format 60 | return HsvToColor(H, S, B); 61 | } 62 | 63 | /// 64 | /// Generates multiple random colors. 65 | /// 66 | /// Which color schemed to use when generating the color. 67 | /// The desired luminosity of the color. 68 | /// How many colors to generate 69 | public static Color[] GetColors(ColorScheme scheme, Luminosity luminosity, int count) 70 | { 71 | var ret = new Color[count]; 72 | for (var i = 0; i < count; i++) 73 | { 74 | ret[i] = GetColor(scheme, luminosity); 75 | } 76 | return ret; 77 | } 78 | 79 | /// 80 | /// Generate one color for each of the provided generator options. 81 | /// 82 | /// List of options to use when creating colors. 83 | public static Color[] GetColors(params Options[] options) 84 | { 85 | if (options == null) throw new ArgumentNullException(nameof(options)); 86 | 87 | return options.Select(o => GetColor(o.ColorScheme, o.Luminosity)).ToArray(); 88 | } 89 | 90 | /// 91 | /// Reseeds the random number generated. 92 | /// 93 | /// The number used to reseed the random number generator. 94 | public static void Seed(int seed) 95 | { 96 | lock (LockObj) 97 | { 98 | _rng = new Random(seed); 99 | } 100 | } 101 | /// 102 | /// Reseeds the random number generated. 103 | /// 104 | public static void Seed() 105 | { 106 | lock (LockObj) 107 | { 108 | _rng = new Random(); 109 | } 110 | } 111 | 112 | private static int PickHue(ColorScheme scheme) 113 | { 114 | var hueRange = GetHueRange(scheme); 115 | var hue = RandomWithin(hueRange); 116 | 117 | // Instead of storing red as two separate ranges, 118 | // we group them, using negative numbers 119 | if (hue < 0) hue = 360 + hue; 120 | 121 | return hue; 122 | } 123 | 124 | private static int PickSaturation(int hue, Luminosity luminosity, ColorScheme scheme) 125 | { 126 | if (scheme == ColorScheme.Monochrome) 127 | { 128 | return 0; 129 | } 130 | 131 | if (luminosity == Luminosity.Random) 132 | { 133 | return RandomWithin(0, 100); 134 | } 135 | 136 | var saturationRange = GetColorInfo(hue).SaturationRange; 137 | 138 | var sMin = saturationRange.Lower; 139 | var sMax = saturationRange.Upper; 140 | 141 | switch (luminosity) 142 | { 143 | case Luminosity.Bright: 144 | sMin = 55; 145 | break; 146 | 147 | case Luminosity.Dark: 148 | sMin = sMax - 10; 149 | break; 150 | 151 | case Luminosity.Light: 152 | sMax = 55; 153 | break; 154 | } 155 | 156 | return RandomWithin(sMin, sMax); 157 | } 158 | 159 | private static int PickBrightness(int H, int S, Luminosity luminosity) 160 | { 161 | var bMin = GetMinimumBrightness(H, S); 162 | var bMax = 100; 163 | 164 | switch (luminosity) 165 | { 166 | case Luminosity.Dark: 167 | bMax = bMin + 20; 168 | break; 169 | 170 | case Luminosity.Light: 171 | bMin = (bMax + bMin) / 2; 172 | break; 173 | 174 | case Luminosity.Random: 175 | bMin = 0; 176 | bMax = 100; 177 | break; 178 | } 179 | 180 | return RandomWithin(bMin, bMax); 181 | } 182 | 183 | private static int GetMinimumBrightness(int H, int S) 184 | { 185 | var lowerBounds = GetColorInfo(H).LowerBounds; 186 | 187 | for (var i = 0; i < lowerBounds.Length - 1; i++) 188 | { 189 | var s1 = lowerBounds[i].X; 190 | var v1 = lowerBounds[i].Y; 191 | 192 | var s2 = lowerBounds[i + 1].X; 193 | var v2 = lowerBounds[i + 1].Y; 194 | 195 | if (S >= s1 && S <= s2) 196 | { 197 | var m = (v2 - v1) / (s2 - s1); 198 | var b = v1 - m * s1; 199 | 200 | return (int)(m * S + b); 201 | } 202 | } 203 | 204 | return 0; 205 | } 206 | 207 | private static Range GetHueRange(ColorScheme colorInput) 208 | { 209 | DefinedColor color; 210 | if (ColorDictionary.TryGetValue(colorInput, out color)) 211 | { 212 | if (color.HueRange != null) 213 | { 214 | return color.HueRange; 215 | } 216 | } 217 | 218 | return new Range(0, 360); 219 | } 220 | 221 | private static DefinedColor GetColorInfo(int hue) 222 | { 223 | // Maps red colors to make picking hue easier 224 | if (hue >= 334 && hue <= 360) 225 | { 226 | hue -= 360; 227 | } 228 | 229 | var ret = ColorDictionary.FirstOrDefault(c => c.Value.HueRange != null && 230 | hue >= c.Value.HueRange[0] && 231 | hue <= c.Value.HueRange[1]); 232 | 233 | Debug.Assert(ret.Value != null); 234 | 235 | return ret.Value; 236 | } 237 | 238 | private static int RandomWithin(Range range) 239 | { 240 | return RandomWithin(range.Lower, range.Upper); 241 | } 242 | private static int RandomWithin(int lower, int upper) 243 | { 244 | lock (LockObj) 245 | { 246 | return _rng.Next(lower, upper + 1); 247 | } 248 | } 249 | 250 | private static void DefineColor(ColorScheme scheme, int[] hueRange, int[,] lowerBounds) 251 | { 252 | int[][] jagged = new int[lowerBounds.GetLength(0)][]; 253 | for (int i = 0; i < lowerBounds.GetLength(0); i++) 254 | { 255 | jagged[i] = new int[lowerBounds.GetLength(1)]; 256 | for (int j = 0; j < lowerBounds.GetLength(1); j++) 257 | { 258 | jagged[i][j] = lowerBounds[i, j]; 259 | } 260 | } 261 | 262 | var sMin = jagged[0][0]; 263 | var sMax = jagged[jagged.Length - 1][0]; 264 | var bMin = jagged[jagged.Length - 1][1]; 265 | var bMax = jagged[0][1]; 266 | 267 | ColorDictionary[scheme] = new DefinedColor() 268 | { 269 | HueRange = Range.ToRange(hueRange), 270 | LowerBounds = jagged.Select(j => new Point(j[0], j[1])).ToArray(), 271 | SaturationRange = new Range(sMin, sMax), 272 | BrightnessRange = new Range(bMin, bMax) 273 | }; 274 | } 275 | 276 | private static void LoadColorBounds() 277 | { 278 | DefineColor( 279 | ColorScheme.Monochrome, 280 | null, 281 | new[,] { { 0, 0 }, { 100, 0 } } 282 | ); 283 | 284 | DefineColor( 285 | ColorScheme.Red, 286 | new[] { -26, 18 }, 287 | new[,] { { 20, 100 }, { 30, 92 }, { 40, 89 }, { 50, 85 }, { 60, 78 }, { 70, 70 }, { 80, 60 }, { 90, 55 }, { 100, 50 } } 288 | ); 289 | 290 | DefineColor( 291 | ColorScheme.Orange, 292 | new[] { 19, 46 }, 293 | new[,] { { 20, 100 }, { 30, 93 }, { 40, 88 }, { 50, 86 }, { 60, 85 }, { 70, 70 }, { 100, 70 } } 294 | ); 295 | 296 | DefineColor( 297 | ColorScheme.Yellow, 298 | new[] { 47, 62 }, 299 | new[,] { { 25, 100 }, { 40, 94 }, { 50, 89 }, { 60, 86 }, { 70, 84 }, { 80, 82 }, { 90, 80 }, { 100, 75 } } 300 | ); 301 | 302 | DefineColor( 303 | ColorScheme.Green, 304 | new[] { 63, 178 }, 305 | new[,] { { 30, 100 }, { 40, 90 }, { 50, 85 }, { 60, 81 }, { 70, 74 }, { 80, 64 }, { 90, 50 }, { 100, 40 } } 306 | ); 307 | 308 | DefineColor( 309 | ColorScheme.Blue, 310 | new[] { 179, 257 }, 311 | new[,] { { 20, 100 }, { 30, 86 }, { 40, 80 }, { 50, 74 }, { 60, 60 }, { 70, 52 }, { 80, 44 }, { 90, 39 }, { 100, 35 } } 312 | ); 313 | 314 | DefineColor( 315 | ColorScheme.Purple, 316 | new[] { 258, 282 }, 317 | new[,] { { 20, 100 }, { 30, 87 }, { 40, 79 }, { 50, 70 }, { 60, 65 }, { 70, 59 }, { 80, 52 }, { 90, 45 }, { 100, 42 } } 318 | ); 319 | 320 | DefineColor( 321 | ColorScheme.Pink, 322 | new[] { 283, 334 }, 323 | new[,] { { 20, 100 }, { 30, 90 }, { 40, 86 }, { 60, 84 }, { 80, 80 }, { 90, 75 }, { 100, 73 } } 324 | ); 325 | } 326 | 327 | /// 328 | /// Converts hue, saturation, and lightness to a color. 329 | /// 330 | public static Color HsvToColor(int hue, int saturation, double value) 331 | { 332 | // this doesn't work for the values of 0 and 360 333 | // here's the hacky fix 334 | var h = Convert.ToDouble(hue); 335 | if (h == 0) 336 | { 337 | h = 1; 338 | } 339 | if (h == 360) 340 | { 341 | h = 359; 342 | } 343 | 344 | // Rebase the h,s,v values 345 | h = h / 360.0; 346 | var s = saturation / 100.0; 347 | var v = value / 100.0; 348 | 349 | var hInt = (int)Math.Floor(h * 6.0); 350 | var f = h * 6 - hInt; 351 | var p = v * (1 - s); 352 | var q = v * (1 - f * s); 353 | var t = v * (1 - (1 - f) * s); 354 | var r = 256.0; 355 | var g = 256.0; 356 | var b = 256.0; 357 | 358 | switch (hInt) 359 | { 360 | case 0: r = v; g = t; b = p; break; 361 | case 1: r = q; g = v; b = p; break; 362 | case 2: r = p; g = v; b = t; break; 363 | case 3: r = p; g = q; b = v; break; 364 | case 4: r = t; g = p; b = v; break; 365 | case 5: r = v; g = p; b = q; break; 366 | } 367 | var c = Color.FromArgb(255, 368 | (byte)Math.Floor(r * 255.0), 369 | (byte)Math.Floor(g * 255.0), 370 | (byte)Math.Floor(b * 255.0)); 371 | 372 | return c; 373 | } 374 | } 375 | } -------------------------------------------------------------------------------- /Common/Range.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | 4 | namespace RandomColorGenerator 5 | { 6 | /// 7 | /// Represents a range using an upper and lower value. 8 | /// 9 | [DebuggerDisplay(@"\{{Lower},{Upper}\}")] 10 | internal class Range 11 | { 12 | public int Lower { get; set; } 13 | public int Upper { get; set; } 14 | 15 | public Range() 16 | { } 17 | public Range(int lower, int upper) 18 | { 19 | Lower = lower; 20 | Upper = upper; 21 | } 22 | 23 | /// 24 | /// Gets the lower range for an index of 0 and the upper for an index of 1. 25 | /// 26 | public int this[int index] 27 | { 28 | get 29 | { 30 | switch (index) 31 | { 32 | case 0: return Lower; 33 | case 1: return Upper; 34 | default: throw new ArgumentOutOfRangeException(); 35 | } 36 | } 37 | set 38 | { 39 | switch (index) 40 | { 41 | case 0: Lower = value; break; 42 | case 1: Upper = value; break; 43 | default: throw new ArgumentOutOfRangeException(); 44 | } 45 | } 46 | } 47 | 48 | internal static Range ToRange(int[] range) 49 | { 50 | if (range == null) return null; 51 | Debug.Assert(range.Length == 2); 52 | return new Range(range[0], range[1]); 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2020 Nathan P Jones 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 | randomColorSharped 2 | ================== 3 | 4 | This is a port to c# (.NET Standard 2.0 and .NET Framework 4.0) of 5 | [randomColor](https://github.com/davidmerfield/randomColor/), David Merfield's 6 | javascript random color generator. This was ported by 7 | [Nathan Jones](http://www.nathanpjones.com/) so that users of the .NET family of 8 | languages could enjoy these attractive colors. 9 | 10 | I saw this project linked on [Scott Hanselman's](http://www.hanselman.com/) 11 | excellent [Newsletter of Wonderful Things](http://www.hanselman.com/newsletter/) 12 | around the same time a coworker was creating an ad hoc visualization app. As we 13 | watched the data appear on screen, we had to squint as some of the colors were 14 | very difficult to make out against the background. This should make things 15 | easier to see and will hopefully help others as well. 16 | 17 | A simple demo in WPF is included so you can play with the various combinations. 18 | 19 | ![Demo App Screenshot](./RandomColorDemoScreenshot.png?raw=true) 20 | 21 | Getting a single color is a simple matter. 22 | 23 | ```csharp 24 | using RandomColorGenerator; 25 | ... 26 | var color = RandomColor.GetColor(ColorScheme.Random, Luminosity.Bright); 27 | ``` 28 | 29 | Or you can generate multiple colors in a single go. 30 | 31 | ```csharp 32 | var colors = RandomColor.GetColors(ColorScheme.Red, Luminosity.Light, 25); 33 | ``` 34 | 35 | # Installing 36 | 37 | randomColorSharped is made available as several NuGet packages depending on your needs. 38 | 39 | #### For .NET Standard 40 | 41 | [randomColorSharped.NetStandard](https://www.nuget.org/packages/randomColorSharped.Forms/) (uses `System.Drawing` added in [.NET Standard 2.0](https://apisof.net/catalog/System.Drawing.Color)) 42 | 43 | ```batch 44 | Install-Package randomColorSharped.NetStandard 45 | ``` 46 | 47 | #### For WinForms on .NET Framework 48 | 49 | [randomColorSharped.Forms](https://www.nuget.org/packages/randomColorSharped.Forms/) (uses `System.Drawing`) 50 | 51 | *NOTE: You should use the NetStandard package above if you're using a version of 52 | .NET Framework that supports it. This Forms package may be deprecated in future.* 53 | 54 | ```batch 55 | Install-Package randomColorSharped.Forms 56 | ``` 57 | 58 | #### For WPF on .NET Framework 59 | [randomColorSharped.WPF](https://www.nuget.org/packages/randomColorSharped.WPF/) (uses `System.Windows`) 60 | 61 | ```batch 62 | Install-Package randomColorSharped.WPF 63 | ``` 64 | -------------------------------------------------------------------------------- /RandomColor.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29806.167 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RandomColorSample", "RandomColorSample\RandomColorSample.csproj", "{C6E267A7-8DEF-4D79-B1F9-1C1BD5821AA0}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RandomColorGenerator.NetStandard", "RandomColorGenerator.NetStandard\RandomColorGenerator.NetStandard.csproj", "{3662A3F5-B30B-4AFE-9443-B146FD517035}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RandomColorGenerator.Forms", "RandomColorGenerator.Forms\RandomColorGenerator.Forms.csproj", "{17EABCDA-D456-4CE6-9E25-1013455F21D4}" 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RandomColorGenerator.WPF", "RandomColorGenerator.WPF\RandomColorGenerator.WPF.csproj", "{997E28E6-E4F7-4B83-9B91-7A7909E4EEBD}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Debug|x86 = Debug|x86 18 | Release|Any CPU = Release|Any CPU 19 | Release|x86 = Release|x86 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {C6E267A7-8DEF-4D79-B1F9-1C1BD5821AA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {C6E267A7-8DEF-4D79-B1F9-1C1BD5821AA0}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {C6E267A7-8DEF-4D79-B1F9-1C1BD5821AA0}.Debug|x86.ActiveCfg = Debug|x86 25 | {C6E267A7-8DEF-4D79-B1F9-1C1BD5821AA0}.Debug|x86.Build.0 = Debug|x86 26 | {C6E267A7-8DEF-4D79-B1F9-1C1BD5821AA0}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {C6E267A7-8DEF-4D79-B1F9-1C1BD5821AA0}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {C6E267A7-8DEF-4D79-B1F9-1C1BD5821AA0}.Release|x86.ActiveCfg = Release|x86 29 | {C6E267A7-8DEF-4D79-B1F9-1C1BD5821AA0}.Release|x86.Build.0 = Release|x86 30 | {3662A3F5-B30B-4AFE-9443-B146FD517035}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {3662A3F5-B30B-4AFE-9443-B146FD517035}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {3662A3F5-B30B-4AFE-9443-B146FD517035}.Debug|x86.ActiveCfg = Debug|Any CPU 33 | {3662A3F5-B30B-4AFE-9443-B146FD517035}.Debug|x86.Build.0 = Debug|Any CPU 34 | {3662A3F5-B30B-4AFE-9443-B146FD517035}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {3662A3F5-B30B-4AFE-9443-B146FD517035}.Release|Any CPU.Build.0 = Release|Any CPU 36 | {3662A3F5-B30B-4AFE-9443-B146FD517035}.Release|x86.ActiveCfg = Release|Any CPU 37 | {3662A3F5-B30B-4AFE-9443-B146FD517035}.Release|x86.Build.0 = Release|Any CPU 38 | {17EABCDA-D456-4CE6-9E25-1013455F21D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 39 | {17EABCDA-D456-4CE6-9E25-1013455F21D4}.Debug|Any CPU.Build.0 = Debug|Any CPU 40 | {17EABCDA-D456-4CE6-9E25-1013455F21D4}.Debug|x86.ActiveCfg = Debug|Any CPU 41 | {17EABCDA-D456-4CE6-9E25-1013455F21D4}.Debug|x86.Build.0 = Debug|Any CPU 42 | {17EABCDA-D456-4CE6-9E25-1013455F21D4}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {17EABCDA-D456-4CE6-9E25-1013455F21D4}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {17EABCDA-D456-4CE6-9E25-1013455F21D4}.Release|x86.ActiveCfg = Release|Any CPU 45 | {17EABCDA-D456-4CE6-9E25-1013455F21D4}.Release|x86.Build.0 = Release|Any CPU 46 | {997E28E6-E4F7-4B83-9B91-7A7909E4EEBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 47 | {997E28E6-E4F7-4B83-9B91-7A7909E4EEBD}.Debug|Any CPU.Build.0 = Debug|Any CPU 48 | {997E28E6-E4F7-4B83-9B91-7A7909E4EEBD}.Debug|x86.ActiveCfg = Debug|Any CPU 49 | {997E28E6-E4F7-4B83-9B91-7A7909E4EEBD}.Debug|x86.Build.0 = Debug|Any CPU 50 | {997E28E6-E4F7-4B83-9B91-7A7909E4EEBD}.Release|Any CPU.ActiveCfg = Release|Any CPU 51 | {997E28E6-E4F7-4B83-9B91-7A7909E4EEBD}.Release|Any CPU.Build.0 = Release|Any CPU 52 | {997E28E6-E4F7-4B83-9B91-7A7909E4EEBD}.Release|x86.ActiveCfg = Release|Any CPU 53 | {997E28E6-E4F7-4B83-9B91-7A7909E4EEBD}.Release|x86.Build.0 = Release|Any CPU 54 | EndGlobalSection 55 | GlobalSection(SolutionProperties) = preSolution 56 | HideSolutionNode = FALSE 57 | EndGlobalSection 58 | GlobalSection(ExtensibilityGlobals) = postSolution 59 | SolutionGuid = {C02C52DD-394E-430E-980C-1202B1D5168D} 60 | EndGlobalSection 61 | EndGlobal 62 | -------------------------------------------------------------------------------- /RandomColorDemoScreenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nathanpjones/randomColorSharped/79919a212d1b1cffbbcddc2a3d3c36b66d4fa864/RandomColorDemoScreenshot.png -------------------------------------------------------------------------------- /RandomColorGenerator.Forms/RandomColorGenerator.Forms.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | net40 4 | win 5 | randomColorSharped.Forms 6 | Nathan P Jones 7 | Nathan P Jones 8 | Generates attractive random colors. 9 | 10 | This is a port to .NET of randomColor (https://github.com/davidmerfield/randomColor), David Merfield&apos;s javascript random color generator. 11 | 12 | Thanks also to Hrusikesh Panda. 13 | random color forms 14 | https://github.com/nathanpjones/randomColorSharped.git 15 | git 16 | https://github.com/nathanpjones/randomColorSharped 17 | LICENSE 18 | Copyright 2014-2020 - Nathan P Jones 19 | 1.0.2 20 | randomColorSharped.Forms 21 | 22 | 23 | TRACE;FORMS 24 | 25 | 26 | TRACE;FORMS 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | True 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /RandomColorGenerator.NetStandard/RandomColorGenerator.NetStandard.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0 5 | Nathan P Jones 6 | randomColorSharped.NetStandard 7 | randomColorSharped.NetStandard 8 | Generates attractive random colors. 9 | 10 | This is a port to .NET of randomColor (https://github.com/davidmerfield/randomColor), David Merfield&apos;s javascript random color generator. 11 | 12 | Thanks also to Hrusikesh Panda. 13 | LICENSE 14 | https://github.com/nathanpjones/randomColorSharped 15 | https://github.com/nathanpjones/randomColorSharped.git 16 | git 17 | random color netstandard 18 | 19 | Copyright 2020 - Nathan P Jones 20 | 1.0.2 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | True 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /RandomColorGenerator.WPF/RandomColorGenerator.WPF.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | net40 4 | win 5 | randomColorSharped.WPF 6 | Nathan P Jones 7 | Nathan P Jones 8 | Generates attractive random colors. 9 | 10 | This is a port to .NET of randomColor (https://github.com/davidmerfield/randomColor), David Merfield&apos;s javascript random color generator. 11 | 12 | Thanks also to Hrusikesh Panda. 13 | random color wpf 14 | https://github.com/nathanpjones/randomColorSharped.git 15 | git 16 | https://github.com/nathanpjones/randomColorSharped 17 | LICENSE 18 | Copyright 2014-2020 - Nathan P Jones 19 | 1.0.2 20 | randomColorSharped.WPF 21 | 22 | 23 | TRACE;WPF 24 | 25 | 26 | TRACE;WPF 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | True 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /RandomColorSample/App.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /RandomColorSample/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Windows; 7 | 8 | namespace RandomColorSample 9 | { 10 | /// 11 | /// Interaction logic for App.xaml 12 | /// 13 | public partial class App : Application 14 | { 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /RandomColorSample/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |