From 57455528f5dc9c800b27f26b750830fc45bdeb32 Mon Sep 17 00:00:00 2001 From: Cameron Taylor Date: Tue, 4 Feb 2025 14:41:10 -0500 Subject: [PATCH] feat: add `luminance` variable to FlxColor (#3357) --- flixel/util/FlxColor.hx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/flixel/util/FlxColor.hx b/flixel/util/FlxColor.hx index 48cab50130..8c6328b84e 100644 --- a/flixel/util/FlxColor.hx +++ b/flixel/util/FlxColor.hx @@ -80,6 +80,12 @@ abstract FlxColor(Int) from Int from UInt to Int to UInt */ public var lightness(get, set):Float; + /** + * The luminance, or "percieved brightness" of a color (from 0 to 1) + * RGB -> Luma calculation from https://www.w3.org/TR/AERT/#color-contrast + */ + public var luminance(get, never):Float; + static var COLOR_REGEX = ~/^(0x|#)(([A-F0-9]{2}){3,4})$/i; /** @@ -741,6 +747,11 @@ abstract FlxColor(Int) from Int from UInt to Int to UInt return maxColor(); } + inline function get_luminance():Float + { + return (redFloat * 299 + greenFloat * 587 + blueFloat * 114) / 1000; + } + inline function get_saturation():Float { return (maxColor() - minColor()) / brightness;