ask a pict its colour?

35 views
Skip to first unread message

Raoul Schorer

unread,
Apr 18, 2020, 8:00:12 AM4/18/20
to Racket Users
Hi,

Using the 'pict' library, I could not find predicates regarding color in the doc.
Is there something like

(get-color (filled-rectangle 30 30)) --> "black"

somewhere?
If not, how can I make a reliable color predicate?

Thanks,
Raoul

Alex Harsanyi

unread,
Apr 18, 2020, 7:49:09 PM4/18/20
to Racket Users

I don't think such a procedure can be written. at least not for the general case.

First, the color of `(filled-rectangle 30 30)` is not black, but whatever color was installed in the drawing context when the filled rectangle was rendered.  You can change it to red using:

(colorize (filled-rectangle 20 20) "red")

Even if you could inspect the colorize pict, it would still not be sufficient for a general `get-color` function.  Users can write their own pict constructors using dc:


(define colors (list "red" "green" "blue" "yellow"))

(define (my-red-colorize other-pict)
 
(dc (lambda (dc dx dy)

       
(define color (list-ref colors (random (length colors))))
       
       
(define old-pen (send dc get-pen))
       
(send dc set-pen (new pen% [width 1] [color color]))
       
       
(draw-pict other-pict dc dx dy)
       
       
(send dc set-pen old-pen))
     
(pict-width other-pict)
     
(pict-height other-pict)))

And a call like `(my-red-colorize (filled-rectangle 20 20))` will only result in a red rectangle about 25% of the time.

Even if you want to ignore cases like the above, what should `get-color` return for the picture below?

(hc-append (colorize (filled-rectangle 20 20) "red") (colorize (filled-rectangle 20 20) "blue"))


Alex.

Robby Findler

unread,
Apr 18, 2020, 10:34:08 PM4/18/20
to Alex Harsanyi, Racket Users
I basically agree with Alex, as the internal structures of pict don't
really keep information in that way, but a "best effort" might be to
draw it into a bitmap and then use the bitmap to find the average
color or the most common color (perhaps with some error bounds on what
colors count as "the same") or something like that.

Robby
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/d3fa9021-63af-4129-bfb6-a82e219cfcf0%40googlegroups.com.

Spencer Florence

unread,
Apr 20, 2020, 11:08:07 AM4/20/20
to Robby Findler, Alex Harsanyi, Racket Users
You could use pict/convert to make a wrapper that remembers the color:

```
#lang racket
(require pict pict/convert)
(struct pict+color (pict color)
  #:property prop:pict-convertible
  (lambda (x) (pict+color-pict x)))

(define (my-colorize p c)
  (pict+color
   (colorize p c)
   c))

(define (get-color p)
  (and
   (pict+color? p)
   (pict+color-color p)))
  ```

This would be a little unreliable, as only the direct result of `my-colorize` would have a retrievable color, but it might work for your purposes.

--spencer

Reply all
Reply to author
Forward
0 new messages