1

I want to make custom GUI object that looks just like a color spectrum . but this color spectrum should be interactive so when a user clicks on any point of that spectrum , the code will relate that point or coordinate of the color spectrum rectangle ,to the rgb value of color of the clicked point.

spectrum bar

the range of the spectrum should be from zero to the maximum possible rgb value (the professor has said to make it from zero to 255^3 which I think is incorrect as 3 can be different colors ) then the range of the the colors should be relate and scaled to the number of pixels the longer side of the rectangle . the clicking part is done by mouse listeners that I've used before but I don't know how to make it to react to several clicks .

so how can I make this object ? also I don't want to use JColorchooser .

Vicarious
  • 131
  • 18
  • 2
    You could take a look at [this example](https://stackoverflow.com/questions/13223065/color-fading-algorithm/13223818#13223818) and [this example](http://stackoverflow.com/questions/21270610/java-smooth-color-transition/21270957#21270957) which uses the same technique to produce a smooth color gradient, but what it can do, is tell you what the color is at any given distance along the gradient. And just because this guy does some awesome stuff which has helped, check out [this blog](http://harmoniccode.blogspot.com.au/2011/04/bilinear-color-interpolation.html) – MadProgrammer Feb 14 '15 at 20:20
  • @MadProgrammer thank you for your answer can you look at [link] (https://stackoverflow.com/questions/28518343/adding-a-slider-with-timer-to-a-grapher-panel?noredirect=1#comment45355612_28518343) this question? It's somehow related to this question – Vicarious Feb 14 '15 at 20:49
  • @MadProgrammer do have any suggestions about the multi-click part? – Vicarious Feb 14 '15 at 22:32
  • You forgot two dimensions, luminance (or brightness) and saturation. Your image above does not contain all possible RBG colors, such as pink, light blue, dark cyan, white, black, etc. What you are looking for is called a "color picker" and most UI frameworks have one builtin. There's no need to go implementing your own. – Jim Garrison Feb 14 '15 at 23:26
  • @JimGarrison that picture was just an example this clickable spectrum bar will be used in a grapher so in an expression like ADD(x , c) user will set the value of x and use c as color variable so each time user clicks on the bar a value assigned to the color that was clicked will be replaced with c and a graph will be drawn – Vicarious Feb 14 '15 at 23:36
  • 1
    Well, the linked examples should allow you to get a Color at a specific point, that's kind of how it all works... – MadProgrammer Feb 15 '15 at 02:13

1 Answers1

2

enter image description here c - is a float number between 0 and 1.

        if(c >= 0 && c <= (1/6.f)){
        red = 255;
        green = 1530 * c;
        blue = 0;
    } else if( c > (1/6.f) && c <= (1/3.f) ){
        red = 255 - (1530 * (c - 1/6f));
        green = 255;
        blue = 0;
    } else if( c > (1/3.f) && c <= (1/2.f)){
        red = 0;
        green = 255;
        blue = 1530 * (c - 1/3f);
    } else if(c > (1/2f) && c <= (2/3f)) {
        red = 0;
        green = 255 - ((c - 0.5f) * 1530);
        blue = 255;
    } else if( c > (2/3f) && c <= (5/6f) ){
        red = (c - (2/3f)) * 1530;
        green = 0;
        blue = 255;
    } else if(c > (5/6f) && c <= 1 ){
        red = 255;
        green = 0;
        blue = 255 - ((c - (5/6f)) * 1530);
    }
Agshin Huseynov
  • 170
  • 3
  • 13
  • What is the significance of the constant `1530` in this answer? It means you end up with negative RGB values, which doesn't make much sense – j b Dec 14 '18 at 16:56
  • 1
    https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/RGB-primaries-in-hue-circle.svg/1280px-RGB-primaries-in-hue-circle.svg.png 1530 = 255 * 6 It is color conversion from HSL to RGB Please tell me value for c that in result my algorithm returns negative value. – Agshin Huseynov Jan 03 '19 at 20:06
  • Thanks for explaining. I thought I saw the possibility for negative values, but it seems I read the code incorrectly. – j b Jan 07 '19 at 16:44