Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module HQ
- open System
- open System.Drawing
- open System.Drawing.Imaging
- type Pixel(red : double, green : double, blue : double, alpha : double) =
- let toInt(value : double) =
- value
- |> Math.Round
- |> Convert.ToInt32
- |> fun i -> if i < 0 then 0 elif i > 255 then 255 else i
- member p.Red = red
- member p.Green = green
- member p.Blue = blue
- member p.Alpha = alpha
- static member FromColor(color : Color) =
- new Pixel(color.R |> Convert.ToDouble,
- color.G |> Convert.ToDouble,
- color.B |> Convert.ToDouble,
- color.A |> Convert.ToDouble)
- member p.ToColor() = Color.FromArgb(alpha |> toInt, red |> toInt, green |> toInt, blue |> toInt)
- type Pixelmap(width : int, height : int, pixels : Pixel[,]) =
- member m.Pixels = pixels
- member m.Width = width
- member m.Height = height
- member m.EachPixel(action) : unit =
- m.Pixels |> Array2D.iteri (fun x y pixel -> action(x, y, pixel))
- static member FromBitmap(bitmap : Bitmap) =
- new Pixelmap(bitmap.Width, bitmap.Height,
- fun x y -> Pixel.FromColor(bitmap.GetPixel(x, y))
- |> Array2D.init bitmap.Width bitmap.Height)
- member m.ToBitmap() : Bitmap =
- let bitmap = new Bitmap(width, height)
- fun(x, y, pixel:Pixel) -> bitmap.SetPixel(x, y, pixel.ToColor())
- |> m.EachPixel
- bitmap
- type Adjust(pixelmap : Pixelmap) =
- let ToGray(pixel:Pixel) = (pixel.Red + pixel.Green + pixel.Blue) / 3.0
- member a.Apply filter =
- fun(x, y, pixel : Pixel) -> pixelmap.Pixels.[x, y] <- filter pixel
- |> pixelmap.EachPixel
- member a.Saturation(value : double) : unit =
- if value <> 0.0 then
- let sat = value + 256.0
- fun (pixel : Pixel) ->
- let gray = pixel |> ToGray
- new Pixel(gray + (pixel.Red - gray) * sat / 255.0,
- gray + (pixel.Green - gray) * sat / 255.0,
- gray + (pixel.Blue - gray) * sat / 255.0, pixel.Alpha)
- |> a.Apply
- member a.Brightness(value : double) : unit =
- if value <> 0.0 then
- fun (pixel : Pixel) ->
- new Pixel(pixel.Red + value,
- pixel.Green + value,
- pixel.Blue + value, pixel.Alpha)
- |> a.Apply
- member a.Grayscale() : unit =
- fun (pixel : Pixel) ->
- let gray = pixel |> ToGray
- new Pixel(gray, gray, gray, pixel.Alpha)
- |> a.Apply
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement