Skip to content

Commit

Permalink
add proposal for color transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
FayCarsons committed Jan 26, 2024
1 parent 9b47680 commit 7f50b7b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lib/shape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ let complex shapes =
match shapes with _ :: _ -> Complex shapes | [] -> Complex []

let rec with_color color = function
| Circle circle -> Circle { circle with color }
| Ellipse ellipse -> Ellipse { ellipse with color }
| Line line -> Line { line with color }
| Circle circle' -> Circle { circle' with color }
| Ellipse ellipse' -> Ellipse { ellipse' with color }
| Line line' -> Line { line' with color }
| Polygon polygon' -> Polygon { polygon' with color }
| Complex complex' -> Complex (List.map (with_color color) complex')
34 changes: 30 additions & 4 deletions lib/transform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ open Shape

type transformation = (shape -> shape)

let rec translate dx dy shape =
match shape with
let rec translate dx dy = function
| Circle circle ->
let dx, dy = (float_of_int dx, float_of_int dy) in
Circle { circle with c = { x = circle.c.x +. dx; y = circle.c.y +. dy } }
Expand All @@ -30,10 +29,16 @@ let rec translate dx dy shape =
}
| Complex shapes -> Complex (List.map (translate dx dy) shapes)

<<<<<<< HEAD
let rec scale factor s =
let scale_length fact len = len *. fact in
let scale_point fact pt = pt *! fact in
match s with
=======
let scale_length fact len = len *. sqrt fact
let scale_point fact pt = pt *! sqrt fact
let rec scale factor = function
>>>>>>> dc851c7 (add proposal for color transforms)
| Circle circle' ->
Circle
{
Expand Down Expand Up @@ -76,8 +81,7 @@ let rotate_point degrees point =
let r, theta = to_polar point in
from_polar (r, theta +. radians)

let rec rotate degrees shape =
match shape with
let rec rotate degrees = function
| Circle circle' -> Circle { circle' with c = rotate_point degrees circle'.c }
| Ellipse ellipse' ->
Ellipse { ellipse' with c = rotate_point degrees ellipse'.c }
Expand All @@ -99,3 +103,25 @@ let repeat n op shape =
in
let shapes = List.fold_right (fun _ acc -> match_list acc) (range n) [] in
complex shapes

(** Takes a function and a shape and returns a new shape with the
function applied to the original's color *)
let rec color_op f = function
| Circle circle' -> Circle { circle' with color = f circle'.color}
| Ellipse ellipse' -> Ellipse { ellipse' with color = f ellipse'.color }
| Line line' -> Line { line' with color = f line'.color}
| Polygon polygon' -> Polygon { polygon' with color = f polygon'.color}
| Complex complex' -> Complex (List.map (color_op f) complex')

(** Linear interpolation *)
let lerp t a b =
let a, b = (float_of_int a, float_of_int b) in
int_of_float ((1. -. t) *. a +. t *. b)

(** Takes float t between 0..1, a color, and a shape
returns a new shape whose color is the linear interpolation between the color
and the shape's color at t *)
let mix t other shape =
let pairwise fn (a, b, c) (d, e, f) = (fn a d, fn b e, fn c f) in
let op = pairwise (lerp t) other in
color_op op shape
8 changes: 0 additions & 8 deletions lib/transform.mli
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
<<<<<<< HEAD
type transformation = (Shape.shape -> Shape.shape)

val translate : int -> int -> transformation
val scale : float -> transformation
val rotate : int -> transformation
val compose : transformation -> transformation -> transformation
val repeat : int -> transformation -> transformation
=======
val translate : float -> float -> Shape.shape -> Shape.shape
val scale : float -> Shape.shape -> Shape.shape
val rotate : int -> Shape.shape -> Shape.shape
val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
val repeat : int -> (Shape.shape -> Shape.shape) -> Shape.shape -> Shape.shape
>>>>>>> e3eb27d (dune fmt)

0 comments on commit 7f50b7b

Please sign in to comment.