diff --git a/DeathSocket/Domain.fs b/DeathSocket/Domain.fs index 834f72c..a6d9276 100644 --- a/DeathSocket/Domain.fs +++ b/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 \ No newline at end of file + | SystemDrawing of string + + type internal longestDimension = + | Width + | Height \ No newline at end of file diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index 59a68b2..f109803 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -55,6 +55,7 @@ namespace DeathSocket printfn "File could not be found at %s" ex.Message } + // NOT TESTED. /// /// 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. /// - 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 /// /// Reads an (jpg or png) image and return its width and height as a diff --git a/DeathSocket/ImageServices.fs b/DeathSocket/ImageServices.fs index 2033b4d..dd5c295 100644 --- a/DeathSocket/ImageServices.fs +++ b/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 ======================================================================== *) diff --git a/DeathSocket/Validation.fs b/DeathSocket/Validation.fs index edc208a..8e3f707 100644 --- a/DeathSocket/Validation.fs +++ b/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 \ No newline at end of file + 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 \ No newline at end of file