BenMakesGames.MonoGame.Extensions 0.1.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package BenMakesGames.MonoGame.Extensions --version 0.1.0
NuGet\Install-Package BenMakesGames.MonoGame.Extensions -Version 0.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BenMakesGames.MonoGame.Extensions" Version="0.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BenMakesGames.MonoGame.Extensions --version 0.1.0
#r "nuget: BenMakesGames.MonoGame.Extensions, 0.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BenMakesGames.MonoGame.Extensions as a Cake Addin
#addin nuget:?package=BenMakesGames.MonoGame.Extensions&version=0.1.0

// Install BenMakesGames.MonoGame.Extensions as a Cake Tool
#tool nuget:?package=BenMakesGames.MonoGame.Extensions&version=0.1.0

What Is It?

BenMakesGames.MonoGame.Extensions contains extension methods for MonoGame's Texture2D and Color classes aimed at texture manipulation. This is a new package, and so the set of methods is still quite small.

Buy Me a Coffee at ko-fi.com


Color Extensions

double EuclideanDistance(this Color a, Color b)

Extension method for Color that returns the "Euclidean distance" to another Color.

"Euclidean distance" means that the R, G, B components are treated as X, Y, Z coordinates, and the distance is the length of the straight line between those coordinates for Color a and Color b. Computing this distance is relatively fast, but does not always yield the most visually-pleasing results.

Example usage:

// compute the Euclidean distance between Goldenrod and SeaGreen:
var distance = Color.Goldenrod.EuclideanDistance(Color.SeaGreen);

Color FindNearestColor(this Color color, IList<Color> palette, Func<Color, Color, double> distanceFunction)

Returns the Color in the palette which is closest to the given color according to the distanceFunction.

This method can be used to reduce a full-color image to a lower color count. Example:

public static readonly Color[] BlackAndWhitePalette = new[] { Color.White, Color.Black };

public static Texture2D MakeBlackAndWhite(Texture2D originalTexture)
{
    return originalTexture.CloneWithFilter(color => color
        .FindNearestColor(BlackAndWhitePalette, ColorExtensions.EuclideanDistance)
    );
}

(Color Min, Color Max) GetMinMax(this IEnumerable<Color> colors)

Returns the minimum and maximum RGB values found within a given collection of colors.

Example usage:

var colors = new Color[] { Color.Red, Color.Blue, Color.Green };
var (min, max) = colors.GetMinMax();

Console.WriteLine($"Min: {min.R}, {min.G}, {min.B}");
Console.WriteLine($"Max: {max.R}, {max.G}, {max.B}");

Color IncreaseBrightness(this Color color, double brightness)

Returns a new color which is a brightened (or darkened) version of the original color. This method uses a simple linear adjustment based on RGB values, so does not always yield the most natural results. You may get better results by converting to HSL, increasing luminosity (L), then converting back to RGB (not currently possible with this library).

Example usage:

var lighterGoldenrod = Color.Goldenrod.IncreaseBrightness(0.5); // 50% increase
var darkerGoldenrod = Color.Goldenrod.IncreaseBrightness(-0.5); // 50% decrease

Color IncreaseContrast(this Color color, double contrast)

Returns a new color which is a more (or less) saturated version of the original color. This method uses a simple linear adjustment based on RGB values, so does not always yield the most natural results. You may get better results by converting to HSL, increasing saturation (S), then converting back to RGB (not currently possible with this library).

Example usage:

var strongerGoldenrod = Color.Goldenrod.IncreaseContrast(0.5); // 50% increase
var mutedGoldenrod = Color.Goldenrod.IncreaseContrast(-0.5); // 50% decrease

Texture2D Extensions

Texture2D Clone(this Texture2D originalTexture)

Returns a clone of the original texture.

Example usage:

Texture2D newTexture = oldTexture.Clone();

// modifying newTexture will not affect oldTexture

Texture2D CloneWithFilter(this Texture2D originalTexture, Func<Color, Color> filter)

Returns a new texture which is a copy of the old texture, with some filter applied to every pixel.

var desaturatedImage = originalTexture.CloneWithFilter(c => c.IncreaseContrast(-1));
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.0 148 5/21/2023