Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Bitmap/F-Sharp/bitmap-1.fs
Normal file
24
Task/Bitmap/F-Sharp/bitmap-1.fs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
//pure functional version ... changing a pixel color provides a new Bitmap
|
||||
type Color = {red: byte; green: byte; blue: byte}
|
||||
type Point = {x:uint32; y:uint32}
|
||||
type Bitmap = {color: Color array; maxX: uint32; maxY: uint32}
|
||||
|
||||
let colorBlack = {red = (byte) 0; green = (byte) 0; blue = (byte) 0}
|
||||
let emptyBitmap = {color = Array.empty; maxX = (uint32) 0; maxY = (uint32) 0}
|
||||
let bitmap (width: uint32) (height: uint32) =
|
||||
match width, height with
|
||||
| 0u,0u | 0u,_ | _, 0u -> emptyBitmap
|
||||
| _,_ -> {color = Array.create ((int) (width * height)) colorBlack;
|
||||
maxX = width;
|
||||
maxY = height}
|
||||
let getPixel point bitmap =
|
||||
match bitmap.color with
|
||||
| c when c |> Array.isEmpty -> None
|
||||
| c when (uint32) c.Length <= (point.y * bitmap.maxY + point.x) -> None
|
||||
| c -> Some c.[(int) (point.y * bitmap.maxY + point.x)]
|
||||
let setPixel point color bitmap =
|
||||
{bitmap with color = bitmap.color |> Array.mapi (function
|
||||
| i when i = (int) (point.y * bitmap.maxY + point.x) ->
|
||||
(fun _ -> color)
|
||||
| _ -> id)}
|
||||
let fill color bitmap = {bitmap with color = bitmap.color |> Array.map (fun _ ->color)}
|
||||
31
Task/Bitmap/F-Sharp/bitmap-2.fs
Normal file
31
Task/Bitmap/F-Sharp/bitmap-2.fs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
//setups
|
||||
//==check pixel for color function
|
||||
let check bitmap color (x,y) =
|
||||
match (getPixel {x=x;y=y} bitmap) with
|
||||
| Some(v) -> v = color
|
||||
| _ -> false
|
||||
let allPixels i j = [for x in [0u..(i-1u)] do for y in [0u..(j-1u)] -> (x,y)]
|
||||
|
||||
//create new empty bitmap
|
||||
let myBitmap = bitmap 0u 0u
|
||||
printfn "Is empty: %b" (myBitmap = emptyBitmap)
|
||||
let myBitmap2 = bitmap 1u 0u
|
||||
printfn "Is empty: %b" (myBitmap2 = emptyBitmap)
|
||||
let myBitmap3 = bitmap 0u 1u
|
||||
printfn "Is empty: %b" (myBitmap3 = emptyBitmap)
|
||||
//create normal bitmap
|
||||
let myBitmap4 = bitmap 14u 14u
|
||||
printfn "Is not empty: %b" (not (myBitmap4 = emptyBitmap))
|
||||
//just check one color
|
||||
printfn "Is 1,1 black: %b" (check myBitmap4 colorBlack (1u,1u))
|
||||
//check out of range color
|
||||
printfn "Is 100,100 nothing: %b" (not(check myBitmap4 colorBlack (100u,100u)))
|
||||
//make sure all pixels are black
|
||||
printfn "Is all black: %b" ((allPixels 14u 14u) |> List.forall (check myBitmap4 colorBlack))
|
||||
//fill bitmap color
|
||||
let colorWhite = {red = (byte) 255; green = (byte) 255; blue = (byte) 255}
|
||||
let myBitmap5 = myBitmap4 |> fill colorWhite
|
||||
printfn "Is all white: %b" ((allPixels 14u 14u) |> List.forall (check myBitmap5 colorWhite))
|
||||
//change just one pixel
|
||||
let myBitmap6 = myBitmap5 |> setPixel {x=5u;y=10u} colorBlack
|
||||
printfn "Is 5,10 black: %b" (check myBitmap4 colorBlack (5u,10u))
|
||||
5
Task/Bitmap/F-Sharp/bitmap-3.fs
Normal file
5
Task/Bitmap/F-Sharp/bitmap-3.fs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
bitmap 14u 14u
|
||||
|> fill {red = (byte) 200; green = (byte) 0; blue = (byte) 10}
|
||||
|> setPixel {x=5u;y=10u} {red = (byte) 0; green = (byte) 0; blue = (byte) 0}
|
||||
|> getPixel {x=5u;y=10u}
|
||||
|> printfn "%A"
|
||||
Loading…
Add table
Add a link
Reference in a new issue