Hi
I guess the problem here is trying to squeeze a lot of information into
a few bits. A single linear slider of 256 pixels' length lets you encode
8 bits, versus the 24 you'd typically want for a direct RGB model.
Simple-mindedly, you could allocate three bits for red and green and two
for blue (or would three for blue and two for green be better?) and have
something like
static final int RED_WIDTH = 3;
static final int GREEN_WIDTH = 3;
static final int BLUE_WIDTH = 2;
static final int MAX_COMPONENT = 255;
static final int RED_MASK = (1 << RED_WIDTH) - 1;
static final int GREEN_MASK = (1 << GREEN_WIDTH) - 1;
static final int BLUE_MASK = (1 << BLUE_WIDTH) - 1;
int red = (x & RED_MASK)* MAX_COMPONENT / RED_MASK;
int green = ((x >>> RED_WIDTH) & GREEN_MASK) * MAX_COMPONENT /
GREEN_MASK;
int blue = ((x >>> (RED_WIDTH+GREEN_WIDTH)) & BLUE_MASK) * MAX_COMPONENT
/ BLUE_MASK;
where, x is the 0.255 offset along the slider.
If you're willing to add another dimension to the slider, you can gain a
few bits by giving it a height (assuming a horizontal slider...) of say,
16 pixels. This buys you 4 bits you can use in the encoding. Using the
above model, you could encode one of the components in the 'y' position
of the slider (which is now a point in a rectangle, rather than a
position along a line), and the other two using the x position, e.g.,
and foregoing all the nice constants,
red = y * 0x11;
green = (x & 0x0f) * 0x11;
blue = ((x >> 4) & 0x0f) * 0x11;
where x is 0..255 and y is 0..15. If you use this approach, though, you
might be better off using a different color model, e.g. HSB. You can
encode H and B in the eight linear bits (not necessarily evenly split),
and the saturation in the four orthogonal bits.
Just some ideas...
Cheers,
Pete
"Downey, Kyle" wrote:
>
> What's the best way to write a linear RGB color picker? I want to use one
> slider rather than three. Any suggested function that will give me a decent
> range across combinations of R, G and B? It doesn't have to be precise. I've
> seen a couple examples of writing simple 3-slider ones both with AWT and
> Java2D, but nothing with a single linear function.
>
> I'm creating a basic Java2D paint program for an art installation, and so
> simplicity of control is more important than precision of color values.
>
> regards,
> kd
>
> ---
> Kyle F. Downey
> Goldman, Sachs & Co. Firmwide Risk of Loss
> 212-357-9067
>
> =====================================================================
> To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
> Java 2D Home Page: http://java.sun.com/products/java-media/2D/
--
Pete Cockerell
California, USA
<http://www.best.com/~petec>
=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 2D Home Page: http://java.sun.com/products/java-media/2D/