Skip to content

Instantly share code, notes, and snippets.

@halcat0x15a
Created March 31, 2011 12:34
Show Gist options
  • Save halcat0x15a/896279 to your computer and use it in GitHub Desktop.
Save halcat0x15a/896279 to your computer and use it in GitHub Desktop.
SVGの色をIntシーケンスに。
private def color(node: Node, attr: String): Seq[Int] = {
val a = """#(.{2})(.{2})(.{2})""".r
val b = """#(.)(.)(.)""".r
val c = """rgb\((\d+)\,(\d+)\,(\d+)\)""".r
val d = """rgb\((\d+)%\,(\d+)%\,(\d+)%\)""".r
node.attribute(attr) map (_.text) orElse {
(node.attribute("style") map { style =>
style.text.split(";") find (_.contains(attr)) map { str =>
str.trim.split(":").last
}
}).get
} map (_ match {
case a(rgb @ _*) => rgb map (Integer.parseInt(_, 16))
case b(rgb @ _*) => rgb map (s => Integer.parseInt(s * 2, 16))
case c(rgb @ _*) => rgb map Integer.parseInt
case d(rgb @ _*) => rgb map (s => (s.toDouble * 255).toInt)
}) getOrElse Seq(0, 0, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment