Created
February 4, 2019 19:57
-
-
Save FremyCompany/2ec404a2821ae46ac762bc5331db8ea4 to your computer and use it in GitHub Desktop.
Output all colors of an image in a file
This file contains hidden or 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
# Open a command prompt and type the following lines: | |
cd C:\type\the\path\to\the\folder\containing\the\cs\file | |
C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe extract-colors.cs /reference:System.Drawing.dll | |
extract-colors.exe > result.txt |
This file contains hidden or 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
using System; | |
using System.Drawing; | |
using System.Collections.Generic; | |
public static class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
// Create a set to store the colors | |
var colors = new HashSet<Color>(); | |
// Load the image | |
var image = new Bitmap( | |
@"C:\image.png", | |
true | |
); | |
// Loop through the images pixels to get their color. | |
for(var x=0; x<image.Width; x++) | |
{ | |
for(var y=0; y<image.Height; y++) | |
{ | |
var pixelColor = image.GetPixel(x, y); | |
// add the color to the set of colors if it is not fully transparent | |
if(pixelColor.A != 0) | |
{ | |
colors.Add(pixelColor); | |
} | |
} | |
} | |
// Loop through the colors and display them in the console | |
foreach(var color in colors) | |
{ | |
Console.WriteLine("#" + color.R.ToString("x2") + color.G.ToString("x2") + color.B.ToString("x2")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment