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 6581875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:12:33+00:00 2026-05-25T16:12:33+00:00

I currently have a simple color picker that loads in a color gradient image

  • 0

I currently have a simple color picker that loads in a color gradient image that I have created (PNG), and when the user hovers or clicks it gets the color value under the cursor.

In the image I have built-in a 14×14 pixel area to the left of the gradient that signifies transparency and when the user hovers or clicks in this area I want to have it clear their color selection (like clicking on transparent). Problem is I can’t figure this part out so I’m looking for anyone’s help.

Hopefully this makes sense.

Here is my code:

var img = new Image();
img.src = '/assets/images/results/colourpicker.png';

// Copy image (img) to canvas
img.onload = function() {
    var c = document.getElementById('colourPickerBar');
    var ctx = c.getContext('2d');
    c.width  = img.width;
    c.height = img.height;
    ctx.drawImage(img,0,0);
}

var rgb;

// get color on hover
$('#colourPickerBar').bind('mousemove', function(e){
    var pos = findPos(this);
    var x = e.pageX - pos.x; // get the x value of the cursor
    var y = e.pageY - pos.y; // get the y value of the cursor
    var ctx = document.getElementById('colourPickerBar').getContext('2d');
    var img_data = ctx.getImageData(x, y, 1, 1).data;
    var hex = "#" + ("000000" + rgbToHex(img_data[0], img_data[1], img_data[2])).slice(-6);

    $('#colourPickerSample').css('background', 'none').css('background-color', hex); //sets the color block value
});

// get color on click
$('#colourPickerBar').bind('click', function(e){
    var pos = findPos(this);
    var x = e.pageX - pos.x; // get the x value of the cursor
    var y = e.pageY - pos.y; // get the y value of the cursor
    var ctx = document.getElementById('colourPickerBar').getContext('2d');
    var img_data = ctx.getImageData(x, y, 1, 1).data;
    var hex = ("000000" + rgbToHex(img_data[0], img_data[1], img_data[2])).slice(-6);

    $('#colourPickerSample').css('background-color', hex); //sets the color block value
    $('#colourSelectorInput').val(hex); //sets the color value in the search input
});

function findPos(obj) {
    var curleft = 0, curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
        return { x: curleft, y: curtop };
    }
    return undefined;
}

function rgbToHex(r, g, b) {
    if (r > 255 || g > 255 || b > 255)
        throw "Invalid color component";
    return ((r << 16) | (g << 8) | b).toString(16);
}

EDIT: Solved thanks to some help below. Now using IF…ELSE statements to decide what action to take when in the region:

// get color on hover
$('#colourPickerBar').bind('mousemove', function(e){
    var pos = findPos(this);
    var x = e.pageX - pos.x; // get the x value of the cursor
    var y = e.pageY - pos.y; // get the y value of the cursor
    var ctx = document.getElementById('colourPickerBar').getContext('2d');
    var img_data = ctx.getImageData(x, y, 1, 1).data;
    var hex = "#" + ("000000" + rgbToHex(img_data[0], img_data[1], img_data[2])).slice(-6);

    if ((0 <= x) && (14 >= x) && (0 <= y) && (14 >= y)) {
         $('#colourPickerSample').css('background-color', 'none').addClass("defaultBg");
    } else {
        $('#colourPickerSample').removeClass("defaultBg").css('background-color', hex);
    }
});

// get color on click
$('#colourPickerBar').bind('click', function(e){
    var pos = findPos(this);
    var x = e.pageX - pos.x; // get the x value of the cursor
    var y = e.pageY - pos.y; // get the y value of the cursor
    var ctx = document.getElementById('colourPickerBar').getContext('2d');
    var img_data = ctx.getImageData(x, y, 1, 1).data;
    var hex = ("000000" + rgbToHex(img_data[0], img_data[1], img_data[2])).slice(-6);

    if ((0 <= x) && (14 >= x) && (0 <= y) && (14 >= y)) {
        $('#colourPickerSample').css('background-color', 'none').addClass("defaultBg");
        $('#colourSelectorInput').val('HEX');
    } else {
        $('#colourPickerSample').removeClass("defaultBg").css('background-color', hex);
        $('#colourSelectorInput').val(hex);
    }
});
  • 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-05-25T16:12:34+00:00Added an answer on May 25, 2026 at 4:12 pm

    You simply must find out where the 14×14 rectangle is and test in your click and move events to see if the mouse is inside of it. If true, do the action you want.

    Since we can’t see your image we can’t tell you precisely where that would be.

    Knowing wether or not the mouse is inside of the 14×14 rect is simply:

    return ((x <= mx) && ((x + width) >= mx) && (y <= my) && ((y + height) >= my));

    Where x,y,width,height are the rect and mx,my are the mouse

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

Sidebar

Related Questions

I currently have a simple form that when you click the save button will
Currently I have a simple maven project that is building a jar file and
I currently have a Perl CGI script that parses incoming XML requests using XML::Simple
I have a (currently very simple) website that is utilising Facebook's Website with Facebook
I am currently programming a background-image using PHP with a color gradient for all
I currently have a simple setup: a centered container div that is 90% width
I have currently created a simple drawing application and was wondering if anybody could
Currently I have a simple knockoutJS object, with a few observables. But this object
I currently have a very simple MySQL database (articlesDB) with 1 table (articles) and
I own a video streaming website and currently I just have a simple hit

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.