Question

I have a sequence of elements [(12, 34); (56, 78); ...] and I want to turn it into [(XXX, XXX, XXX); (XXX, XXX, XXX); ...] where (XXX, XXX, XXX) is the (R, G, B) of the given tuple/coordinates, e.g., (12, 34). According to .NET documentation, System.Drawing.Bitmap.GetPixel returns System.Drawing.Color and you can just call Color.R, Color.B and Color.G to get the colour value as a byte.

This is what I've got so far:

let neighbourColor (img : System.Drawing.Bitmap) (s : seq<int * int>) =
 s |> Seq.collect(fun e -> s |> img.GetPixel(fst(e), snd(e)) |> Seq.map(fun c -> (c.R, c.G, c.B)))

For every element e in sequence s, get the color of e which is a tuple of coordinates on image img, then pass it to Seq.map to transform it into (Red, Green, Blue) and collect it as a new sequence neighbourColor.

Basically I want to turn seq<int*int> into seq<byte*byte*byte>.

Was it helpful?

Solution

let neighbourColor (img:Bitmap) coords =
   coords |> Seq.map (fun (x, y) -> let c = img.GetPixel (x, y) in c.R, c.G, c.B)

In general, it is probably better style to only explicitly specify a parameter's type when it cannot be inferred (in this case coords is already implicitly understood to be a seq<int*int>).

OTHER TIPS

let (|RGB|) (c:Color)=RGB(c.R,c.G,c.B)
let Test (img:Bitmap)=Seq.map (img.GetPixel>>function RGB(r,g,b)->r,g,b)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top