Skip to content

Commit

Permalink
feat: add function to calculate the percieved brightness of a FlxColor
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjamuffin99 committed Feb 4, 2025
1 parent 348f108 commit d427a02
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 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 @@ -355,6 +361,8 @@ abstract FlxColor(Int) from Int from UInt to Int to UInt
return {color1: this, color2: triadic1, color3: triadic2};
}



/**
* Return a 24 bit version of this color (i.e. without an alpha value)
*
Expand Down Expand Up @@ -741,6 +749,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

0 comments on commit d427a02

Please sign in to comment.