Skip to content

getBrightness() set to work with RGB format only - should be available for any format #5

Open
@stam66

Description

@stam66

As it stands getBrightness(pRGB) is set up for RGB formatted colours only, and by extension this applies to isLight() and isDark() as well.

This creates unnecessary work in liveCode to pass a colour formatted as RGB - for example if a control has had a colour attribute set by colorName or hex then this is passed to isDark() for example, the developer has to write extra code to translate this to RGB format (if you don't you get an error in the messageBox).

This is easily fixable by passing a color (rather than specifically RBG) and just making it the tinyColor sColor, from which the RBG is seamlessly extracted.

With the principle of minimising any changes to existing code, the handler could look like this:

function getBrightness pColor
    // http://www.w3.org/TR/AERT#color-contrast
    local tRGB
    get tinyColor(pColor, empty)
    put _fixRGB(toRGB(true)) into tRGB 
    split tRGB by comma
    return ((tRGB[1]* 299) + (tRGB[2]* 587) + (tRGB[3]* 114)) / 1000
end getBrightness

This works seamlessly with any color format passed - but i don't understand the _fixRBG() handler.
If the RBG values are revived directly from the tinyColor sColor this shouldn't be needed but have left in as i don't understand it.

if it isn't needed now, the handler could be a bit simpler:

function getBrightness pColor
    // http://www.w3.org/TR/AERT#color-contrast
    local tRGB
    get tinyColor(pColor, empty)
    put toRGB(true) into tRGB 
    split tRGB by comma
    return ((tRGB[1]* 299) + (tRGB[2]* 587) + (tRGB[3]* 114)) / 1000
end getBrightness

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions