Skip to content

Commit

Permalink
Merge pull request #3 from michaldivis/vNext
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
michaldivis committed Dec 21, 2022
2 parents f10daba + acbcdd6 commit d8270b3
Show file tree
Hide file tree
Showing 27 changed files with 1,015 additions and 128 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<img src="assets/icon.png?raw=true" width="200">
<img src="assets/icon.png?raw=true" width="200">

# Dark Colors

Expand Down Expand Up @@ -88,4 +88,30 @@ var twoColorsCombined = ColorBlender.Combine(baseColor, colorToAdd);
var baseColor = Color.FromArgb(0, 0, 0);
var colorToAdd = Color.FromArgb(125, 55, 13);
var twoColorsCombined = baseColor.Combine(colorToAdd);
```
```

## Color analyzer

⚠️ Warning: this feature is pretty slow at the moment. It can process roughly one mega pixel per second, so it's not recommended to use it on large images.

Find dominant color in an image.

<img src="assets/sample_screenshot_analyzer.png?raw=true">

### Basic usage
```csharp
//array of pixels that represents an image
Color[] pixels;
//returns a list of dominant color candidates, ordered by probability
List<DominantColorCandidate> candidates = ColorAnalyzer.FindDominantColors(pixels);
```

### Advanced (with configuration)
```csharp
Color[] pixels;
List<DominantColorCandidate> candidates = ColorAnalyzer.FindDominantColors(pixels, options =>
{
options.MinSaturation = 0.4f,
options.MinSpaceCoverage = 0.05f,
options.ColorGrouping = 0.3f
});
Binary file added assets/sample_screenshot_analyzer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions sample/SampleApp/BitmapUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using SkiaSharp.Views.Desktop;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Media.Imaging;

namespace SampleApp;

public static class BitmapUtils
{
public static Color[] GetPixels(Bitmap bitmap)
{
return bitmap.ToSKBitmap().Pixels
.Select(x => x.ToDrawingColor())
.ToArray();
}

public static BitmapImage BitmapToImageSource(Bitmap bitmap)
{
using MemoryStream memory = new MemoryStream();

bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();

return bitmapimage;
}
}
58 changes: 58 additions & 0 deletions sample/SampleApp/ColorAnalyzerOptionsExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DarkColors;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Windows.Media.Imaging;

namespace SampleApp;

public partial class ColorAnalyzerExample : ObservableObject
{
public class DemoImage
{
public BitmapImage? BitmapSource { get; set; }
public Color[] Pixels { get; set; }
public ObservableCollection<DominantColorCandidate> Candidates { get; set; } = new();
}

public DominantColorAnalyzerOptions Options { get; } = new()
{
MaxCandidateCount = 5
};

public ObservableCollection<DemoImage> Images { get; } = new();

public ColorAnalyzerExample(Bitmap[] bitmaps)
{
var images = bitmaps.Select(x => new DemoImage
{
BitmapSource = BitmapUtils.BitmapToImageSource(x),
Pixels = BitmapUtils.GetPixels(x)
});

foreach (var image in images)
{
Images.Add(image);
}

UpdateCandidates();
}

[RelayCommand]
private void UpdateCandidates()
{
foreach (var result in Images)
{
result.Candidates.Clear();

var candidates = ColorAnalyzer.FindDominantColors(result.Pixels, Options);

foreach (var candidate in candidates)
{
result.Candidates.Add(candidate);
}
}
}
}
2 changes: 1 addition & 1 deletion sample/SampleApp/ColorBlendingExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public ColorBlendingExample(string Title, Color baseColor, params ColorLayer[] l
Layers = layers;
ResultColor = ColorBlender.Combine(baseColor, layers);
}
}
}
22 changes: 14 additions & 8 deletions sample/SampleApp/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
using DarkColors;
using SampleApp.Properties;
using System.Collections.Generic;
using System.Drawing;

namespace SampleApp;

public class MainViewModel
{
public List<ColorBlendingExample> Examples { get; } = new();
public List<ColorBlendingExample> ColorBlendingExamples { get; } = new();
public ColorAnalyzerExample ColorAnalyzerExample { get; }

public MainViewModel()
{
Examples.Add(new ColorBlendingExample("A bit of gray", Hex("#000"), new ColorLayer(Hex("#fff"), 25)));
Examples.Add(new ColorBlendingExample("Fifty shades", Hex("#000"), new ColorLayer(Hex("#fff"), 50)));
Examples.Add(new ColorBlendingExample("Bright green", Hex("#007d40"), new ColorLayer(Hex("#fff"), 75)));
ColorBlendingExamples.Add(new ColorBlendingExample("A bit of gray", Hex("#000"), new ColorLayer(Hex("#fff"), 25)));
ColorBlendingExamples.Add(new ColorBlendingExample("Fifty shades", Hex("#000"), new ColorLayer(Hex("#fff"), 50)));
ColorBlendingExamples.Add(new ColorBlendingExample("Bright green", Hex("#007d40"), new ColorLayer(Hex("#fff"), 75)));

Examples.Add(new ColorBlendingExample("Is it blue or purple?", Hex("#000"), new ColorLayer(Hex("#4056F4"), 60)));
ColorBlendingExamples.Add(new ColorBlendingExample("Is it blue or purple?", Hex("#000"), new ColorLayer(Hex("#4056F4"), 60)));

Examples.Add(new ColorBlendingExample("More than two colors", Hex("#007d40"), new ColorLayer[]
ColorBlendingExamples.Add(new ColorBlendingExample("More than two colors", Hex("#007d40"), new ColorLayer[]
{
new ColorLayer(Hex("#4056F4"), 42),
new ColorLayer(Hex("#B1740F"), 28)
}));

Examples.Add(new ColorBlendingExample("Both alpha transparency and amount percentage used", Hex("#000"), new ColorLayer(Hex("#804056F4"), 55)));
ColorBlendingExamples.Add(new ColorBlendingExample("Both alpha transparency and amount percentage used", Hex("#000"), new ColorLayer(Hex("#804056F4"), 55)));

Examples.Add(new ColorBlendingExample("Combo - both alpha transparency and amount percentage used for multiple colors", Hex("#007d40"), new ColorLayer[]
ColorBlendingExamples.Add(new ColorBlendingExample("Combo - both alpha transparency and amount percentage used for multiple colors", Hex("#007d40"), new ColorLayer[]
{
new ColorLayer(Hex("#804056F4"), 71),
new ColorLayer(Hex("#5CB1740F"), 20)
}));

var demoBitmaps = new[] { Resources.vildhjarta, Resources.rivers, Resources.abovebelow, Resources.greylotus, Resources.currents, Resources.dali, Resources.metallica, Resources.northlane };

ColorAnalyzerExample = new(demoBitmaps);
}

private static Color Hex(string hex)
Expand Down
Loading

0 comments on commit d8270b3

Please sign in to comment.