Wednesday, 11 September 2013

Color wheel

The following program uses our F# for Visualization library to draw a color wheel:

open System.Windows
open FlyingFrog.Graphics

let brush(r, g, b) =
  let f x = 256.0 * x |> max 0.0 |> min 255.0 |> byte
  Media.SolidColorBrush(Media.Color.FromRgb(f r, f g, f b))

let polygon brush xys =
  Shape([Interior brush], [ Contour.Closed[for x, y in xys -> line_to x y] ])

let wheel m n =
 let a = System.Math.PI / float n
  let ka, sa = cos a, sin a
  let ta = (ka + sa) / (ka - sa)
  let d = ka + sa * ta
  let m = float m
  Group
    [ for e in 0.0..m-1.0 do
        let r = 1.0 / (ka - sa) ** e
        for i in 0..n-1 do
          let t = a * (float(2*i) + e)
          let k, s = r * cos t, r * sin t
          yield
            polygon (brush(float i / float n, 0.5*(1.0 + e/m), 0.5*(2.0 - e/m)))
              [ k, s
                d*k*ka - d*s*sa, d*s*ka + d*k*sa
                ta*k, ta*s
                d*k*ka + d*s*sa, d*s*ka - d*k*sa ] ]

View(wheel 12 16)

No comments: