Skip to content

Instantly share code, notes, and snippets.

@ToJans
Created June 24, 2014 07:51
Show Gist options
  • Save ToJans/5f7ce1de4b723041d59a to your computer and use it in GitHub Desktop.
Save ToJans/5f7ce1de4b723041d59a to your computer and use it in GitHub Desktop.
type RgbColor = {r:int;g:int;b:int} with
static member create r g b = {r=r;g=g;b=b}
static member fmap f left right = {r=f left.r right.r;g=f left.g left.b; b = f left.b right.b }
static member (-) (l,r) = RgbColor.fmap (-) l r
static member (*) (l,r) = RgbColor.fmap (*) l r
member left.squaredDistance right =
let diffColor = (left - right)
let squaredColor = diffColor * diffColor
diffColor.r + diffColor.g + diffColor.b
type NamedColor = {name:string;color:RgbColor} with
static member create name r g b = {name=name;color = RgbColor.create r g b}
let similarColors (knownColors:NamedColor seq) (maxCount:int) (colorFrom:RgbColor):NamedColor seq =
knownColors
|> Seq.sortBy (fun x -> x.color.squaredDistance colorFrom)
|> Seq.take maxCount
// this should probably come from a database
let knownColors =
[
NamedColor.create "Black" 0 0 0
NamedColor.create "Blue" 0 0 255
NamedColor.create "Green" 0 255 0
NamedColor.create "Cyan" 0 255 255
NamedColor.create "Red" 255 0 0
NamedColor.create "Magenta" 255 0 255
NamedColor.create "Yellow" 255 255 0
NamedColor.create "White" 255 255 255
]
RgbColor.create 12 50 255 |> similarColors knownColors 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment