Last active
February 14, 2019 22:49
-
-
Save vexx32/605b27e5ff842d7f0aa5a772c0740f42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type TransformToSKColorAttribute() = | |
inherit ArgumentTransformationAttribute() | |
let matchColor name = | |
seq { | |
match name with | |
| knownColor when ColorNames.Contains(knownColor) -> yield ColorLibrary.[knownColor] | |
| clear when String.Equals(clear, "transparent", StringComparison.OrdinalIgnoreCase) -> yield SKColor.Empty | |
| patternString when WildcardPattern.ContainsWildcardCharacters(patternString) -> | |
let pattern = WildcardPattern(patternString, WildcardOptions.IgnoreCase) | |
for color in ColorNames do | |
if pattern.IsMatch(color) then yield ColorLibrary.[color] | |
| x -> | |
let (success, color) = SKColor.TryParse(x) | |
if success then yield color | |
} | |
let transform (input : obj) = | |
let values = | |
match input with | |
| :? Array as arr -> arr | |
| x -> Seq.toArray [ x ] :> Array | |
seq { | |
for item in values do | |
match item with | |
| :? string as s -> for color in matchColor s do yield color | |
| x -> | |
let properties = | |
match x with | |
| :? IDictionary as d -> d :> IEnumerable | |
| p -> PSObject.AsPSObject(p).Properties :> IEnumerable | |
let red = if isNull (ValueFrom properties "Red") then 0uy else ValueFrom properties "Red" |> As<byte> | |
let green = if isNull (ValueFrom properties "Green") then 0uy else ValueFrom properties "Green" |> As<byte> | |
let blue = if isNull (ValueFrom properties "Blue") then 0uy else ValueFrom properties "Blue" |> As<byte> | |
let alpha = if isNull (ValueFrom properties "Alpha") then 255uy else ValueFrom properties "Alpha" |> As<byte> | |
yield SKColor(red, green, blue, alpha) | |
} | |
override self.Transform(intrinsics, data) = | |
let results = | |
match data with | |
| :? SKColor as c -> seq { yield c } | |
| :? string as s -> matchColor s | |
| x -> transform x | |
|> Seq.toList | |
match results with | |
| unit when unit.Length = 1 -> unit.[0] :> obj | |
| x -> x :> obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment