Browse Source

refactored scale line thickness functionality.

master
Craig Oates 5 years ago
parent
commit
c04598ad68
  1. 6
      DeathSocket/Domain.fs
  2. 8
      DeathSocket/GridPainter.fs
  3. 25
      DeathSocket/ImageServices.fs
  4. 23
      DeathSocket/Validation.fs

6
DeathSocket/Domain.fs

@ -167,4 +167,8 @@
/// Denotes an image created using SkiaSharp
| SkiaSharp of string
/// Denotes an image created using System.Drawing
| SystemDrawing of string
| SystemDrawing of string
type internal longestDimension =
| Width
| Height

8
DeathSocket/GridPainter.fs

@ -55,6 +55,7 @@ namespace DeathSocket
printfn "File could not be found at %s" ex.Message
}
// NOT TESTED.
/// <summary>
/// Determines the current scale an image is viewed at (E.G. scaled
/// preview in image viewer). The (pen) line thickness is then updated
@ -76,8 +77,11 @@ namespace DeathSocket
/// SkiaSharp based functions already scale the grid lines for you. So,
/// you should not need to use this function when using them.
/// </remarks>
let scaleLineThickness (previewDimension: double) (actualDimension: double) (lineThickness: double) =
setLineThickness previewDimension actualDimension lineThickness
let scaleLineThickness (previewDimensions: int * int) (actualDimensions: int * int) (lineThickness: double) =
validateDimensions previewDimensions |> ignore
validateDimensions actualDimensions |> ignore
validateLineThickness |> ignore
adjustLineThickness previewDimensions actualDimensions lineThickness
/// <summary>
/// Reads an (jpg or png) image and return its width and height as a

25
DeathSocket/ImageServices.fs

@ -1,12 +1,13 @@
module internal ImageServices
open System
open System.IO
open System.Drawing
open System.Drawing.Imaging
open DeathSocket
open Validation
open ColourServices
open SkiaSharp
open System
(* Note on the Use of Repeated Code
===========================================================================
@ -20,10 +21,24 @@
lurking in an end users RAM can grind their computer to a halt. On top of
that, they have no means to make alterations to the code. *)
let setLineThickness pDimension aDimension lineWidth =
if (pDimension <= 0.0 || aDimension <= 0.0 || lineWidth <= 0.0) then
raise (new DivideByZeroException ("[ERROR] The images height and width must be greater than 0."))
else lineWidth / (pDimension / aDimension)
let determineImageScale pDimension aDimension =
(double aDimension) / (double pDimension)
let determineLineScale (lineWidth: double) (scale: double) =
lineWidth / scale
let adjustLineThickness pDimensions aDimensions lineWidth =
match (validateLongestDimension pDimensions aDimensions) with
| Width ->
let thickness =
determineImageScale (fst pDimensions) (fst aDimensions)
|> determineLineScale lineWidth
thickness
| Height ->
let thickness =
determineImageScale (snd pDimensions) (snd aDimensions)
|> determineLineScale lineWidth
thickness
(* SkiaSharp Functions
======================================================================== *)

23
DeathSocket/Validation.fs

@ -1,11 +1,12 @@
module internal Validation
open System.IO
open DeathSocket
let validateFilePath path =
match File.Exists path with
| true -> ()
| false -> raise (new FileNotFoundException (path + " could not be found."))
| false -> raise (new FileNotFoundException ("No file found at " + path))
let validateSaveFileType file =
match Path.GetExtension file with
@ -17,4 +18,22 @@
let validateIO iPath oPath =
validateFilePath iPath
validateSaveFileType oPath
validateSaveFileType oPath
let validateDimensions dimensions =
match dimensions with
| (0, _) -> invalidArg "Width" "Width must be greater than 0."
| (_, 0) -> invalidArg "Height" "Height must be greater than 0."
| (_, _) -> ()
let validateLineThickness thickness =
match thickness with
| thickness when thickness <= 0.0 -> invalidArg "LineThickness" "LineThickness must be greater than 0."
| _ -> ()
let validateLongestDimension pDims aDims =
let width = (fst pDims) - (fst aDims)
let height = (snd pDims) - (snd aDims)
match width >= height with
| true -> Width
| false -> Height
Loading…
Cancel
Save