NetPalette 0.0.2
dotnet add package NetPalette --version 0.0.2
NuGet\Install-Package NetPalette -Version 0.0.2
<PackageReference Include="NetPalette" Version="0.0.2" />
paket add NetPalette --version 0.0.2
#r "nuget: NetPalette, 0.0.2"
// Install NetPalette as a Cake Addin #addin nuget:?package=NetPalette&version=0.0.2 // Install NetPalette as a Cake Tool #tool nuget:?package=NetPalette&version=0.0.2
<img src="https://raw.githubusercontent.com/nor0x/NetPalette/main/Art/packageicon.png" width="320px" />
NetPalette 🎨
A Library for generating color palettes from images - powered by SkiaSharp so it can be used in any modern .NET UI framework. Inspired by the Android Palette API
Demo
There is a .NET MAUI app in /Samples which demonstrates palette generation for random Unsplash images. The library is not limited to MAUI, it can be used in any .NET UI framework like Uno, Avalonia, WinUI, WPF, etc. any framework that supports SkiaSharp.
https://github.com/nor0x/NetPalette/assets/3210391/dea64c88-354f-458f-bf04-9e915d801a43
Usage
Palette Generation
To generate a palette from a bitmap image, you can use the FromBitmap method of the PaletteGenerator class. This method takes in various parameters to customize the palette generation process
using SkiaSharp;
// Load your bitmap image
SKBitmap bitmap = SKBitmap.Decode("path/to/your/image.png");
// Define region of interest if necessary - this is optional
SKRect region = new SKRect(0, 0, bitmap.Width, bitmap.Height); // Entire image
// Optionally define palette filters - this is optional
List<IPaletteFilter> filters = new List<IPaletteFilter> { new AnyPaletteFilter() };
// Optionally define palette targets - this is optional
List<PaletteTarget> targets = new List<PaletteTarget> { PaletteTarget.Vibrant, PaletteTarget.DarkMuted };
// Generate the palette
PaletteGenerator paletteGenerator = PaletteGenerator.FromBitmap(bitmap, maximumColorCount: 16, region, filters, targets, fillMissingTargets: true);
// Access the generated palette colors
SKColor vibrantColor = paletteGenerator.VibrantColor.Color;
SKColor darkMutedColor = paletteGenerator.DarkMutedColor.Color;
// Iterate over all quantized colors
foreach (SKColor color in paletteGenerator.Colors)
{
// Do something with each color
}
Accessing Generated Palette Colors
You can access the generated palette colors using the properties provided by the PaletteGenerator instance.
// Accessing specific palette colors
SKColor vibrantColor = paletteGenerator.VibrantColor.Color;
SKColor lightVibrantColor = paletteGenerator.LightVibrantColor.Color;
SKColor darkVibrantColor = paletteGenerator.DarkVibrantColor.Color;
SKColor mutedColor = paletteGenerator.MutedColor.Color;
SKColor lightMutedColor = paletteGenerator.LightMutedColor.Color;
SKColor darkMutedColor = paletteGenerator.DarkMutedColor.Color;
SKColor dominantColor = paletteGenerator.DominantColor.Color;
// Iterate over palette colors
foreach (SKColor color in paletteGenerator.PaletteColors)
{
// Do something with each palette color
}
// Iterate over all quantized colors
foreach (SKColor color in paletteGenerator.Colors)
{
// Do something with each color
}
API
Create a PaletteGenerator instance using the FromBitmap method of the PaletteGenerator class. This method takes in the following parameters:
encodedImage
(SKBitmap): The bitmap image from which to generate the palette.maximumColorCount
(int, optional): The maximum number of colors to include in the palette. Default is 16.region
(SKRect, optional): The region of interest within the image. Default is the entire image.filters
(List<IPaletteFilter>, optional): Optional palette filters to apply during palette generation.targets
(List<PaletteTarget>, optional): Optional palette targets to specify which colors to select from the generated palette.fillMissingBaseTargets
(bool, optional): Specifies whether to fill missing base targets in the selected swatches. Default is false.
Returns
A PaletteGenerator
instance containing the generated palette.
Exceptions
ArgumentOutOfRangeException
: Thrown when the specified region is outside the bounds of the image.ArgumentException
: Thrown when the image byte data doesn't match the image size or has invalid encoding.
not finalized yet
please note that the API is not finalized yet and might change in the future. If you have any suggestions or feature requests, feel free to open an issue or a pull request.
Credits
most of the code is based on the palette_generator package from the Flutter SDK - credits to the original authors of that code. Great kudos also to the SkiaSharp contributors for providing such a great library 👏
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- SkiaSharp (>= 2.88.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.