Skip to content

Instantly share code, notes, and snippets.

@RenaissanceBug
Created September 20, 2015 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RenaissanceBug/12374cfee674c63b260f to your computer and use it in GitHub Desktop.
Save RenaissanceBug/12374cfee674c63b260f to your computer and use it in GitHub Desktop.
Choosing colors with Racket
#lang racket/base
(require 2htdp/image
lang/posn
racket/match)
;; Given: I've got three colors of paint, and the wall is painted light blue.
(define blue (make-color #x66 #xcc #xff))
(define purple (make-color #x66 #x33 #xff))
(define orange (make-color #xff #xcc #x33))
(define green (make-color #x33 #xff #xcc))
;; Goal: decide how to combine those colors for an illustration of the
;; Pythagorean distance formula.
(define W 400)
(define H 300)
(define x0 130) ; x-coordinate of the Y-axis
(define y0 200) ; and vice versa
;; bl, tr = bottom-left and top-right points
(define bl (make-posn (- x0 30) (- y0 50)))
(define tr (make-posn (+ x0 200) (- y0 120)))
;; Wall and (in green) axes:
(define BG
(scene+line (scene+line (rectangle W H 'solid blue) x0 0 x0 H green)
0 y0 W y0 green))
;; Color Image -> Image : draw the line between the two points
(define (connect-points color bg)
(scene+line bg (posn-x bl) (posn-y bl) (posn-x tr) (posn-y tr) color))
;; Color Image -> Image : draw vert/horiz lines representing delta-x and delta-y
(define (dx-dy-lines color bg)
(define dash-pen (make-pen color 2 'long-dash 'round 'round))
(scene+line (scene+line bg
(posn-x bl) (posn-y bl) (posn-x tr) (posn-y bl)
dash-pen)
(posn-x tr) (posn-y tr) (posn-x tr) (posn-y bl)
dash-pen))
;; Color Image -> Image : draw the two points.
(define (points color bg)
(place-image (circle 2 'solid color) (posn-x bl) (posn-y bl)
(place-image (circle 2 'solid color)
(posn-x tr) (posn-y tr)
bg)))
;; Color^3 -> Image : try a combo of 3 colors
(define (test-scheme c1 c2 c3)
(points c1 (connect-points c2 (dx-dy-lines c3 BG))))
;; Try all the combinations:
(for*/list ([pt-color `((orange ,orange) (purple ,purple))]
[line-color `((orange ,orange) (purple ,purple))]
[dx-dy-color `((green ,green) (orange ,orange))])
; At first glance I ruled out green for the points and line, and purple
; for the dx and dy lines.
(match (list pt-color line-color dx-dy-color)
[(list (list c1n c1) (list c2n c2) (list c3n c3))
(list (format "Axis lines: ~a / Lines: ~a / Points: ~a" c3n c2n c1n)
(test-scheme c1 c2 c3))]))
;; Final decision: purple points, yellow connecting line, green dashed lines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment