|
|
|
module internal InternalServices
|
|
|
|
|
|
|
|
open System.IO
|
|
|
|
open System.Drawing
|
|
|
|
open SmoulderingBeachBall.Domain
|
|
|
|
|
|
|
|
module Validation =
|
|
|
|
|
|
|
|
let validateDimension dimension =
|
|
|
|
match dimension with
|
|
|
|
| dimension when dimension <= 0 ->
|
|
|
|
invalidArg "dimension" "The width and height must be greater than 0."
|
|
|
|
| _ -> ()
|
|
|
|
|
|
|
|
let validateDirectory filePath =
|
|
|
|
let path = Path.GetDirectoryName filePath
|
|
|
|
match (Directory.Exists path) with
|
|
|
|
| false -> invalidArg "filePath" "Unable to save to the specified location because it does not exist."
|
|
|
|
| true -> ()
|
|
|
|
|
|
|
|
module Drawing =
|
|
|
|
open System.Text
|
|
|
|
|
|
|
|
let penOffset penWidth = int (penWidth / (float32 2))
|
|
|
|
|
|
|
|
let createBorderPath penWidth spec =
|
|
|
|
let offset = penOffset penWidth
|
|
|
|
[|Point (0, offset); // Essentially (0, 0)
|
|
|
|
Point ((spec.width - offset), offset);
|
|
|
|
Point ((spec.width - offset), (spec.height - offset));
|
|
|
|
Point (offset, (spec.height - offset));
|
|
|
|
Point (offset, 0)|]
|
|
|
|
|
|
|
|
let drawBorder (graphics: Graphics) (pen: Pen) spec =
|
|
|
|
printfn "[INFO.] Adding border to image..."
|
|
|
|
let penPath = createBorderPath pen.Width spec
|
|
|
|
graphics.DrawLines (pen, penPath)
|
|
|
|
|
|
|
|
|
|
|
|
let drawFullOverlay (graphics: Graphics) (pen: Pen) spec =
|
|
|
|
drawBorder graphics pen spec
|
|
|
|
printfn "[INFO.] Adding full overlay to image..."
|
|
|
|
let offset = penOffset pen.Width
|
|
|
|
// Draws line from top-left to bottom-right of square.
|
|
|
|
graphics.DrawLine (pen, offset, offset, (spec.width - offset), (spec.height - offset))
|
|
|
|
// Draws line from top-right to bottom-left of square.
|
|
|
|
graphics.DrawLine (pen, (spec.width - offset), offset, offset, (spec.height - offset))
|
|
|
|
|
|
|
|
let addOverlayToImage graphics spec =
|
|
|
|
let overlay = spec.overlay.Value
|
|
|
|
let pen = new Pen (overlay.colour, Width = 10.0f)
|
|
|
|
match overlay.overlayType with
|
|
|
|
| Border -> drawBorder graphics pen spec
|
|
|
|
| Full -> drawFullOverlay graphics pen spec
|
|
|
|
|
|
|
|
let buildFileName spec =
|
|
|
|
let sb = new StringBuilder ()
|
|
|
|
sb.Append (spec.width.ToString ()) |> ignore
|
|
|
|
sb.Append "x" |> ignore
|
|
|
|
sb.Append (spec.height.ToString ()) |> ignore
|
|
|
|
sb.Append ".png" |> ignore
|
|
|
|
(sb.ToString ())
|
|
|
|
|
|
|
|
|
|
|
|
let drawImage spec =
|
|
|
|
let bitmap = new Bitmap (spec.width, spec.height)
|
|
|
|
let graphics = Graphics.FromImage (bitmap)
|
|
|
|
let rectangle = Rectangle (0, 0, spec.width, spec.height)
|
|
|
|
graphics.FillRectangle (spec.colour, rectangle)
|
|
|
|
match spec.overlay.IsSome with
|
|
|
|
| true -> addOverlayToImage graphics spec
|
|
|
|
| false -> printfn "[INFO.] No overlay specified. Creating image without one."
|
|
|
|
bitmap.Save (Path.Combine (spec.filePath, (buildFileName spec)))
|
|
|
|
graphics.Dispose()
|
|
|
|
bitmap.Dispose()
|