TIL how to convert hex triplet to `racket/draw` color

spdegabrielle
Tue, 21 Oct 2025 13:11:45 +0000
General

TIL how to convert a hex triplet to racket/draw color

;; Convert hex triplet to `racket/draw` color
;; hex-triplet->color% : hex -> colour%
(define (hex-triplet->color% x)
  (define-values [r g b]
    (values (arithmetic-shift x -16)
            (bitwise-and #x0000ff (arithmetic-shift x -8))
            (bitwise-and #x0000ff x)))
  (make-color r g b))

> (hex-triplet->color% #xdf542f)
(object:color% ...)

from https://gitlab.com/bengreenman/pict-abbrevs/-/blob/master/private/pict-abbrevs.rkt#L188 thanks @ben_greenman

1 post - 1 participant