Fun with colors

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:

AC_ColorTools!

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:

RGBColor:=RGBColor_FromCMYK(Cyan;Magenta;Yellow;Kontrast)

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


RGBColor:=RGBColor_FromHSL(Hue;Saturation;Luminosity)

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.


RGBColor:=RGBColor_FromHSV(Hue;Saturation;Value)

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.


RGBColor:=RGBColor_FromHTML(HTMLColor)

Accepting a valid HTML color code you get the RGB longint for it


RGBColor:=RGBColor_FromRGB(Red;Green;Blue)

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


Brightness:=RGBColor_GetBrightness(Color{;AsPercent})

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.


Median:=RGBColor_GetMedian(Color{;AsPercent})

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


Color:=RGBColor_Shade(Color;Percentage)

Will calculate a new shade of the color passed.
Give it a real with the percentage (0-100 or 0-1)


RGBColor_ShadeArray(ArrayPointer;BaseColor{;MaxStep})

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


RGB_ColorToCMYK(RGBColor;-Cyan;->Magenta;-Yellow;->Kontrast)

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)


RGBColor_ToHSL(RGBColor;->Hue;->Saturation;->Luminosity)

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)


RGBColor_ToHSV(RGBColor;->Hue;->Saturation;->Value)

See above


WebColorCode:=RGBColor_ToHTML(4D_RGBColor)

Will return an HTML HEX code (#RRGGBB) for the RGB longint passed


RGBColor_ToRGB(Color;->Red;->Green;->Blue)

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:

AC_ColorTools V14

AC_ColorTools V15

AC_ColorTools V16

As always, feedback, praises, criticism and bug reports welcome…

3 thoughts on “Fun with colors

  1. Foucauld Pérotin

    Nice try! 🙂 but it cannot be useful for “print” tools. There you specify colours in CMYK, but cannot get the correct RGB colour to display it, by the direct simple formula. The toolbox (macOS for instance) can do that, but with 4D you have to use a plugin, to get the good colour and display it. Else, you RGB colour will be really far from reality.

    Take a look a my example with a purple C50 M80. The colour picker of the Mac do it well, my 4D software that uses a plugin too, but the RGB colour you calculate is rather far from the one you will have, when printing this CMYK colour.

    http://teluric.com/demo_color.png

    (actually I found your page, because I have to find a cross platform plugin, because mine is mac only…)

    Reply
    1. admin Post author

      Sorry, late in seeing this…
      You are welcome to modify my code, the goal was not to have printable colors, it was made for the screen.
      And here I find the results satisfactory. As a photographer, I know quite well that calculating CMYK values from RGB is quite an art, and if you need to take into account specific printer profiles, you can imagine the world of pain you are in.
      So I will not touch that.

      Reply

Leave a Reply to Pat Bensky Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.