Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add luminance variable to FlxColor #3357

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions flixel/util/FlxColor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand Down