It has become increasingly difficult to sell customers on the idea of amber text on a blackish screen, everybody expects brilliant, dazzling colors in Kodachrome…
Now 4D does work with colors and when defining colors you have the choice between using catchy names like light sable, dead otter or bami-goreng backwards and using a nomenclature that is a little bit like web colors, just replace the hash with 0x00 and you are set, easy as pie.
Now, out there, there are a lot of different color systems and (surprisingly) none is the one 4D uses!
There is RGB, CMYK, HSL, HSV and probably a few more I cannot be bothered with right now.
Now the interesting question:
How to convert between these systems and what about finding other information, like :
- What’s the brightness of my color, useful for determining if text on it should be white or black
- What does my color look like if its a tad darker
- What do 50 shades of my color look like?
Well, do not despair, I obviously have the solution to the problem you never knew you had:
If you start the Component by itself it will display this elaborate screen:
Mostly self-explaining I hope. Please ignore the spelling errors…
Either click on the button to select a color with the system color picker or enter something sensible in any of the fields and see magic happen On Data Change (i.e. once you leave the field).
The button +5% and -5% will calculate a lighter or darker shade of your color.
Contrary to my other components this one has quite a few commands, this is their documentation, in alphabetical order:
Takes the values for Cyan, Magenta, Yellow and Black (Kontrast I know I know, it comes from german), all in percentage fractions (real | 0-1), and thus as reals and returns the corresponding RGB longint
Accepting the values for Hue (degree | real | 0-360) Saturation (% | real | 0-1) and Luminosity (% | real | 0-1) it will return the corresponding RGB longint.
Accepting the values for Hue (degree | real | 0-360) Saturation (% | real | 0-1) and Value (% | real | 0-1) it will return the corresponding RGB longint.
Accepting a valid HTML color code you get the RGB longint for it
Pass along the 3 values for Red, Green and Blue (longint with an 8-bit value 0-255) and you will get an RGB longint
Pass it an RGB color (longint) and you will get the brightness back, either as 8-bit value 0-255 or as percentage if you pass True in the optional second parameter. Return value is a real.
This is quite useful for determining if a text on a color should be white or black. Setting it to white if the brightness falls below 140 has proven quite usable, you mileage may vary.
calculates tho color median for a given color as median between the Red Green Blue components, returned as 8bit value (0-255), or as percentage (0-1) if you pass True in the optional second parameter
Will calculate a new shade of the color passed.
Give it a real with the percentage (0-100 or 0-1)
Will fill an array with shades of you color, both up and down. please make sure you set the dimension of the array first and that the array is of type longint. Setting a max step percentage (0-100) will limit the size of each shading step
This one takes an RGB color and will return, in the real variables the pointers show to, the 4 values for Cyan, Magenta, Yellow and Black as percentages (0-1)
I think you get it by now, don’t you?
Hue is in degrees, 0-360, the other will give you a percentage in a real (0-1)
Will return an HTML HEX code (#RRGGBB) for the RGB longint passed
Ze final one takes an RGB color and will return, in the numeric variables the pointers show to, the 3 values for Red, Green and Blue as 8-bit values (0-255)
Important: As you have by now noticed in my code, error checking for the values passed is quite nonexistent, so program defensively.
This one is a bit heavier than my other stuff, so the source is available upon request, pressing the donate button in the sidebar with anything above 5$ will expedite the sending of the source.
So, without further ado, here are the components for download:
As always, feedback, praises, criticism and bug reports welcome…