javascriptjqueryalgorithmrgbcolor-wheel

jQuery - Get White or Dark Text over Color Wheel


This problem popped up when I looked at an almost perfect bit of jQuery code called Jar Two Wheel Color Picker:

http://www.jar2.net/projects/jquery-wheelcolorpicker/demo

If you notice in their demo when you move your cursor over the wheel, the developer appears to be using an inverse color algorithm that's slightly imperfect. I mean, if you use it and paste in 8072ff, you end up with something you can't read in the textbox.

The fix in my mind was to change the algorithm so that it shows black when over lighter colors, and white when over darker colors.

I looked into the source and found where it's updating the color of the textbox field. There, I'm able to get the R, G, and B color values from 0 to 255.

In Javascript and jQuery, knowing the R, G, and B values of a background color, how can I switch the foreground color to either white or black so that it is readable?


Solution

  • Given the RGB/HSV conversion functions from Michael Jackson and this bit of HTML to play with:

    <input type="text" id="pancakes">
    <button id="margie">go</button>
    

    Then something like this might be a decent starting point:

    $('#margie').click(function() {
        var bg  = $('#pancakes').val();
        var rgb = bg.match(/../g);
        for(var i = 0; i < 3; ++i)
            rgb[i] = parseInt(rgb[i], 16);
        var hsv = rgbToHsv(rgb[0], rgb[1], rgb[2]);
        var fg  = 'ffffff';
        if(hsv[2] > 0.5)
            fg = '000000';
        $('#pancakes').css({
            color: '#' + fg,
            background: '#' + bg
        });
    });
    

    Demo: http://jsfiddle.net/ambiguous/xyA4a/

    You might want to play with the hsv[2] > 0.5 and possibly look at the hue and saturation as well but just a simple value check should get you started. You might want to play with HSL rather than HSV and see which one works better for you.