Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8341273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:25:51+00:00 2026-06-09T05:25:51+00:00

Edit: Posting Solution I wanted to create color swatches based on the color chosen

  • 0

Edit: Posting Solution

I wanted to create color swatches based on the color chosen in Farbtastic. I did my calculations on the HSL value, because it’s easier and produces better results.

After getting the HSL value from Farbtastic, I added brightness to create a new color. The new color is in HSL format, and I need to switch it back to RGB or Hex, in order to display it and save it for use later. Some browsers display HSL, but I have no faith that all mobile browsers etc will do so.

The problem was to convert variable ‘newcolor’ back to RGB or Hex, once the calculation was done.

// collect parent ids for farb
$(".farbtastic").parent().each(writeColorpanel);

function writeColorpanel (i, elem) {
    var picker = $.farbtastic(elem);  //farb object
    picker.linkTo(onColorChange); //a farb function
}  

    function onColorChange(color) {

    // retrieve hsl value, do calculations and place in hidden input.
    var hslcolor = $.farbtastic('#example_colorpicker_picker').hsl;

    // brighten by 40%
    var brightness = 1.4 * (Math.round(hslcolor[2]*10000)/10000);
    if (brightness>1) { brightness = 1 };
    if (brightness<0) { brightness = 0; }

    // create the new brighter color
    var newcolor = hslcolor;
    newcolor[2]=brightness;

    //convert to rgb (safer than trusting all mobile browsers to display HSL)
    var rgb = hsl2rgb(newcolor);

    //apply the color to swatches
    var firstSwatch = $('#section-example_colorpicker').find('.square1');
    firstSwatch.css( {'background-color': color } );
    var secondSwatch = $('#section-example_colorpicker').find('.square2');
    secondSwatch.css('background-color', 'rgb('+rgb.r+','+rgb.g+','+rgb.b+')');

}

function hsl2rgb(hsl) {
    var h = hsl[0], s = hsl[1], l = hsl[2];
    var m1, m2, hue;
    var r, g, b
    h = (Math.round( 360*h )/1);
    if (s == 0)
        r = g = b = (l * 255);
    else {
        if (l <= 0.5)
            m2 = l * (s + 1);
        else
            m2 = l + s - l * s;
        m1 = l * 2 - m2;
        hue = h / 360;
        r = Math.round(HueToRgb(m1, m2, hue + 1/3));
        g = Math.round(HueToRgb(m1, m2, hue));
        b = Math.round(HueToRgb(m1, m2, hue - 1/3));
    }
    return {r: r, g: g, b: b};
}

function HueToRgb(m1, m2, hue) {
    var v;
    if (hue < 0)
        hue += 1;
    else if (hue > 1)
        hue -= 1;

    if (6 * hue < 1)
        v = m1 + (m2 - m1) * hue * 6;
    else if (2 * hue < 1)
        v = m2;
    else if (3 * hue < 2)
        v = m1 + (m2 - m1) * (2/3 - hue) * 6;
    else
        v = m1;

    return 255 * v;
}

Note that the script marked as answer works for whole number values. Since Farb returned fractional values, I made minor edits, posted above.

WordPress Theme authors:
If you are using Options Framework Theme for WordPress, and want to use Farbtastic so you may use HSL values, see this Fork by Elihorn, which contains the files you need. In this set of files I needed to add “wp_enqueue_style( ‘farbtastic’ ); ” at the line where we enqueued the script. The .JS file for farbtastic does not need to be included because WP already has it. You’ll also need to enqueue Jquery, of course, probably at the top of functions.php.

For part2 of this question (an issue with duplicates), see:
Return different values from function used multiple times

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:25:54+00:00Added an answer on June 9, 2026 at 5:25 am

    I decided to seperate the functions and rewrite them a little so they work properly.
    Ended up with this :

    function hsl2rgb(h, s, l) {
        var m1, m2, hue;
        var r, g, b
        s /=100;
        l /= 100;
        if (s == 0)
            r = g = b = (l * 255);
        else {
            if (l <= 0.5)
                m2 = l * (s + 1);
            else
                m2 = l + s - l * s;
            m1 = l * 2 - m2;
            hue = h / 360;
            r = Math.round(HueToRgb(m1, m2, hue + 1/3));
            g = Math.round(HueToRgb(m1, m2, hue));
            b = Math.round(HueToRgb(m1, m2, hue - 1/3));
        }
        return {r: r, g: g, b: b};
    }
    
    function HueToRgb(m1, m2, hue) {
        var v;
        if (hue < 0)
            hue += 1;
        else if (hue > 1)
            hue -= 1;
    
        if (6 * hue < 1)
            v = m1 + (m2 - m1) * hue * 6;
        else if (2 * hue < 1)
            v = m2;
        else if (3 * hue < 2)
            v = m1 + (m2 - m1) * (2/3 - hue) * 6;
        else
            v = m1;
    
        return 255 * v;
    }
    

    It’s called by doing:

    //the HSL values to use
    var h = 30;
    var s = 20;
    var l = 70;
    
    //call the function into a variable, returns an object rgb = {r:163, g:174, b:196}
    var rgb = hsl2rgb(h, s, l);
    
    //can now be accessed with rgb.r etc, and remember that the last two are percentages
    

    Here’s a DEMONSTRATION

    If your variable newcolor is a string, you will have to do:

    var colorArray = newcolor.split(','),
        h = colorArray[0],  //  0.10434092941291336
        s = colorArray[1],  //  1
        l = colorArray[2];  //  0.756
    

    to get the HSL values into the right variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: I've learned much since the posting of this question, and so I have
EDIT: Simple version of the question: I want to create server variables in the
I would like to create some kind of open free group posting of photos
EDIT #1: so SOLUTION IS : line MPI_Gatherv(buffer, rank, MPI_INT, buffer, receive_counts, receive_displacements, MPI_INT,
EDIT: Sorted. See bottom for solution Okay, I have to write a program that
-Edit- FYI.. I am converting b&w documents scanned in as greyscale or color. 1)The
This is fairly 'math-y' but I'm posting here because it's a Project Euler problem,
This is my first time posting here, so please be kind ;-) EDIT My
I'm posting this more because I'd like to learn more, because my work-around has
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.