Math of Colors

Michael Mantkowski michaelj at clientrax.com
Tue Nov 19 11:47:57 EST 2019


Thanks to Geir and Phil!

The addition of the Intensity code solved the issues I was seeing.  Still a few colors are up to interpretation to my eyes, but anything that is glaringly wrong is caught be the code below.  Works great!

I modified the result to be 0 or 1 so that I could interpret it to be either "Dark" or "Light" text rather than "Black" or "White".

Calculate pvColor as truergb(pvColor)

Calculate lvRed as mod(pvColor,256)
Calculate lvGreen as mod(int(pvColor/256),256)
Calculate lvBlue as mod(int(pvColor/(256*256)),256)

Calculate lvIntensity as (lvRed*0.299)+(lvGreen*0.114)+(lvBlue*0.114)

Calculate lvVarRed as lvRed/255
Calculate lvVarGreen as lvGreen/255
Calculate lvVarBlue as lvBlue/255

If lvVarRed<0.03928
    Calculate lvVarRed as lvVarRed/12.92
Else
    Calculate lvVarRed as pwr(((lvVarRed+0.055)/1.055),2.4)
End If

If lvVarGreen<0.03928
    Calculate lvVarGreen as lvVarGreen/12.92
Else
    Calculate lvVarGreen as pwr(((lvVarGreen+0.055)/1.055),2.4)
End If

If lvVarBlue<0.03928
    Calculate lvVarBlue as lvVarBlue/12.92
Else
    Calculate lvVarBlue as pwr(((lvVarBlue+0.055)/1.055),2.4)
End If

Calculate lvVarL as 0.2126*lvVarRed+0.7152*lvVarGreen+0.0722*lvVarBlue

Quit method pick(lvVarL>.179,1,0)

*********************************************************************
Michael Mantkowski
ClienTrax Software
1-614-875-2245
*********************************************************************


-----Original Message-----
From: omnisdev-en On Behalf Of Michael Mantkowski
Sent: Tuesday, November 19, 2019 10:53 AM
To: 'OmnisDev List - English' <omnisdev-en at lists.omnis-dev.com>
Subject: RE: Math of Colors

Thanks Phil,

I found that as well and I am probably going to need to do this.  I used the first example from Geir to do this...  It works in most cases.  But sometimes what looks acceptable with a mostly blue color is not with a red color even though they come up with the same value.  So, I think we need to go further than the simple "intensity < x".

Calculate pvColor as truergb(pvColor)

Calculate lvRed as mod(pvColor,256)
Calculate lvGreen as mod(int(pvColor/256),256) Calculate lvBlue as mod(int(pvColor/(256*256)),256)

Calculate lvIntensity as (lvRed*0.299)+(lvGreen*0.114)+(lvBlue*0.114)

*********************************************************************
Michael Mantkowski
ClienTrax Software
1-614-875-2245
*********************************************************************


-----Original Message-----
From: omnisdev-en On Behalf Of Phil (OmnisList) via omnisdev-en
Sent: Tuesday, November 19, 2019 10:33 AM
To: omnisdev-en at lists.omnis-dev.com
Cc: Phil (OmnisList) <phil at pgpotter.co.uk>
Subject: Re: Math of Colors

Hi Michael,

I also found this interesting...

example:
https://codepen.io/znak/pen/aOvMOd

To save you some time...

This is what I got:

;  From the colour selected in pnColour, it calculates the best text colour for this background colour, as per W3C Recommendations... ;; Only returns Black or white.

Calculate lnRed as mod(pnColour,256)
Calculate lnGreen as mod(int(pnColour/256),256) Calculate lnBlue as mod(int(pnColour/(256*256)),256)

Calculate lnVarRed as lnRed/255
Calculate lnVarGreen as lnGreen/255
Calculate lnVarBlue as lnBlue/255

If lnVarRed<0.03928
     Calculate lnVarRed as lnVarRed/12.92 Else
     Calculate lnVarRed as pwr(((lnVarRed+0.055)/1.055),2.4) End If

If lnVarGreen<0.03928
     Calculate lnVarGreen as lnVarGreen/12.92 Else
     Calculate lnVarGreen as pwr(((lnVarGreen+0.055)/1.055),2.4)
End If

If lnVarBlue<0.03928
     Calculate lnVarBlue as lnVarBlue/12.92 Else
     Calculate lnVarBlue as pwr(((lnVarBlue+0.055)/1.055),2.4)
End If

Calculate lnVarL as 0.2126*lnVarRed+0.7152*lnVarGreen+0.0722*lnVarBlue
If lnVarL>0.179
     Quit method kBlack
Else
     Quit method kWhite
End If

No.    Local Variable    Type    Subtype    Init.Val/Calc Description
1    lnBlue    Short integer    (0 to 255)
2    lnGreen    Short integer    (0 to 255)
3    lnRed    Short integer    (0 to 255)
4    lnVarBlue    Number    floating dp
5    lnVarGreen    Number    floating dp
6    lnVarL    Number    floating dp
7    lnVarRed    Number    floating dp

regards
Phil Potter
Based in Chester in the UK.

On 19/11/2019 13:46, Michael Mantkowski wrote:
> Hi Geir!
>
> Thank you so much for this.  I need to do some studying here but looks 
> like just what I was looking for.
>
> Thanks,
>
> Michael
>
> *********************************************************************
> Michael Mantkowski
> ClienTrax Software
> 1-614-875-2245
> *********************************************************************
>
>
> -----Original Message-----
> From: omnisdev-en On Behalf Of Geir Fjærli
> Sent: Tuesday, November 19, 2019 8:37 AM
> To: OmnisDev List - English <omnisdev-en at lists.omnis-dev.com>
> Subject: Re: Math of Colors
>
> Hi Michael.
>
> Generally, blue is darker and green the lighter of the colours, with 
> red in between, and so they need to be weighted differently. Sto 
> something like
> this:
>
> if (red*0.299 + green*0.587 + blue*0.114) > 186 use black.
>
> See this Stackioverflow thread for more:
>
> https://stackoverflow.com/questions/3942878/how-to-decide-font-color-i
> n-whit e-or-black-depending-on-background-color
> <https://stackoverflow.com/questions/3942878/how-to-decide-font-color-
> in-whi
> te-or-black-depending-on-background-color>
>
> Geir :)
>
>> 19. nov. 2019 kl. 13:14 skrev Michael Mantkowski <michaelj at clientrax.com>:
>>
>> Does anyone out there understand the "Math of Colors"
>>
>> I am looking for a way to know by either the RGB value or the numeric 
>> value of a color if it is dark enough to require any text under it to 
>> be a lighter color.
>>
>> We have designed an interface for our application so that the user 
>> can select the color of the basic elements of a Window.  Background 
>> color, Field Color and Container Field Color.
>>
>> I don't want it to get overly complex however and would rather be 
>> able to detect if the element they selected is too dark to work with 
>> the standard text color (which is normally black) of those elements.
>> If I could figure this out, then I could automatically change the 
>> text color of the objects with the dark background colors.
>>
>> Is this possible or should I just bite the bullet and provide a text 
>> color selection with each background color option?
>>
>> Thanks,
>>
>> Michael
>>
_____________________________________________________________
Manage your list subscriptions at http://lists.omnis-dev.com Start a new message -> mailto:omnisdev-en at lists.omnis-dev.com 

_____________________________________________________________
Manage your list subscriptions at http://lists.omnis-dev.com Start a new message -> mailto:omnisdev-en at lists.omnis-dev.com 




More information about the omnisdev-en mailing list