Making a colorpicker

edgar edgar-rft at web.de
Thu Aug 15 08:08:01 UTC 2019


Am Wed, 14 Aug 2019 20:49:30 +0200
schrieb Tom <trashtalk217 at gmail.com>:

Answer follows after Tom's text...

> Hi,
> 
> I'm back from vacation and continuing my exploration of ltk. I'm 
> currently trying to build a color picker and I'm modeling it after
> the blender color picker. To replicate how it looks I need a nice
> gradient. My first instinct was to just put a bunch of really small
> rectangles (pixels) on a canvas. This worked (see attachment), but it
> took about 5 minutes for all the pixels to be put on the canvas. Not
> ideal, especially if I have to redraw the gradient when the
> brightness value is changed by some slider or something. My next idea
> was to use a bitmap. This leads my to my first question: How do I get
> a bitmap on a canvas? I tried the following code.
> 
> ```
> 
> (defvar image (open "lena_gray.bmp"))
> 
> (with-ltk ()
>    (let* ((canvas (make-instance 'canvas
>                  :width 400
>                  :height 400)))
>      (pack canvas)
>      (let ((lena (create-bitmap canvas 20 20 :bitmap image)))
>        (declare (ignore lena)))))
> 
> ```
> 
> And I get a generic type-error:
> 
> The value
>    NIL
> is not of type
>    STREAM
> when binding STREAM
>     [Condition of type TYPE-ERROR]The value
>    NIL
> is not of type
>    STREAM
> when binding STREAM
>     [Condition of type TYPE-ERROR]
> 
> There is really no example given in the documentation. So I need help 
> with this one (Using SBCL on Linux by the way).
> 
> The next question is concerning transparent colors. To darken the 
> gradient I thought of having an oval over the bitmap, on which I can 
> change the transparency value. I haven't found a way to do that with 
> "itemconfigure". And I also haven't found a place where all the
> keyword arguments are listed for "itemconfigure". I'm almost certain
> that there are no transparency options for canvas items in tcl/tk.
> But in case I've overlooked them, I'm asking anyway.
> 
> I'd also like to say that I accept tips on my approach. Is there an 
> obvious solution that I'm missing or are there other general tips I
> need to figure it out myself. Please let me know! Thanks in advance
> for any help.
> 
> Greetings, Tom.

Hi Tom,

In Tk, a BITMAP is a 2-color image, and a PHOTO is an image with
more than 2 colors. Tk comes with built-in handlers for PPM/PGM,
PNG and GIF image formats.

The full details about Tk images are here:

-> https://www.tcl.tk/man/tcl8.6/TkCmd/image.htm
-> https://www.tcl.tk/man/tcl8.6/TkCmd/bitmap.htm
-> https://www.tcl.tk/man/tcl8.6/TkCmd/photo.htm

Tk also comes with its own color picker, see:

-> https://www.tcl.tk/man/tcl8.6/TkCmd/chooseColor.htm

I haven't worked with LTK for a while now, but there is a Lisp variable
that makes LTK print the Tcl/Tk code it generates. I usually had looked
at the generated code and compared it with the examples on the Tcl/Tk
man pages when I had no clue how to get what I wanted.

There's also a Tcl/Tk wiki with tons of code examples:

-> https://wiki.tcl-lang.org/

Hope this helps a bit... :-)

- edgar





More information about the ltk-user mailing list