Recreate specific colour

If you're having trouble using Context Free or don't understand the language, ask for help here.

Moderators: chris, MtnViewMark, MtnViewJohn

Post Reply
Tlyman
Posts: 7
Joined: Thu Sep 20, 2007 7:45 am

Recreate specific colour

Post by Tlyman » Sat May 26, 2018 2:16 am

I have an SVG image containing a specific colour which I would like to use. I looked up the HSL values of it in an SVG editor (Inkscape).

Now when I try to recreate that colour to use it in a primitive shape by mapping from the SVG ranges to the CFDG ones, the hue is correct, but the S and L values are lower. An example:

Code: Select all

startshape colour_test

shape colour_test {
    svg_h = 128
    svg_s = 172
    svg_l = 128
    CIRCLE [ hue (svg_h / 255 * 359) saturation (svg_s / 255) brightness (svg_l / 255) ]
}
When generating SVG from that with the cfdg command, the resulting colour is HSL (128, 129, 85).

The documentation says that the saturation and brightness adjustments change the existing values by the given percentage towards 1. But as the initial values are 0, in my understanding the result should be the provided target values.

What am I missing?

Tlyman
Posts: 7
Joined: Thu Sep 20, 2007 7:45 am

Re: Recreate specific colour

Post by Tlyman » Sat May 26, 2018 6:23 am

Never mind, I solved it :)

I extracted the HSBColor constructor and hacked it into a small command-line tool which reads RGBA values and outputs the corresponding HSBA values used by cfdg.

When I use these for my shape, it's coloured as expected (once I figured out that the input RGBA values must have a range from 0 to 1 instead of from 0 to 255 ;) ).

Still, this issue might be worth mentioning in the documentation because the behaviour is rather not obvious.

User avatar
MtnViewJohn
Site Admin
Posts: 842
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: Recreate specific colour

Post by MtnViewJohn » Sun May 27, 2018 2:33 pm

There are actually two functions: hsb2rgb() and rgb2hsb() that convert between HSB and RGB. But I forgot to document them. Context Free uses HSB/HSV, which is similar to HSL but not exactly the same. Also, there is a utility dialog called the Color Calculator that can be used to manually convert between RGB and HSB. It is under the Window menu on Mac and a different menu on Windows (edit menu maybe).

Tlyman
Posts: 7
Joined: Thu Sep 20, 2007 7:45 am

Re: Recreate specific colour

Post by Tlyman » Fri Jun 22, 2018 1:50 pm

Good to know for next time, thanks! Maybe you'll find the time to update the documentation :) I didn't even know that there's a difference between HSB and HSL.

As I'm on Linux and didn't want to bother with compiling, I just used the command line tool from the package manager and couldn't try that Color Calculator :)

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests