Mercurial > code > home > repos > homeauto
changeset 139:5b607e8d35d0
add color/hex
Ignore-this: 20a75656647a5049527ba255437a8542
author | drewp@bigasterisk.com |
---|---|
date | Sat, 12 Jul 2014 11:32:00 -0700 |
parents | cc04b6af0477 |
children | 89bc300e1419 |
files | service/pilight/src/color/hex/colorhex.go |
diffstat | 1 files changed, 58 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/service/pilight/src/color/hex/colorhex.go Sat Jul 12 11:32:00 2014 -0700 @@ -0,0 +1,58 @@ +// From https://github.com/zond/wildlife/blob/master/code.google.com/p/gorilla/color/hex.go + +// Copyright 2012 The Gorilla Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package hex + +import ( + "fmt" + "image/color" + "strconv" +) + +// HexModel converts any Color to an Hex color. +var HexModel = color.ModelFunc(hexModel) + +// Hex represents an RGB color in hexadecimal format. +// +// The length must be 3 or 6 characters, preceded or not by a '#'. +type Hex string + +// RGBA returns the alpha-premultiplied red, green, blue and alpha values +// for the Hex. +func (c Hex) RGBA() (uint32, uint32, uint32, uint32) { + r, g, b := HexToRGB(c) + return uint32(r) * 0x101, uint32(g) * 0x101, uint32(b) * 0x101, 0xffff +} + +// hexModel converts a Color to Hex. +func hexModel(c color.Color) color.Color { + if _, ok := c.(Hex); ok { + return c + } + r, g, b, _ := c.RGBA() + return RGBToHex(uint8(r>>8), uint8(g>>8), uint8(b>>8)) +} + +// RGBToHex converts an RGB triple to an Hex string. +func RGBToHex(r, g, b uint8) Hex { + return Hex(fmt.Sprintf("#%02X%02X%02X", r, g, b)) +} + +// HexToRGB converts an Hex string to a RGB triple. +func HexToRGB(h Hex) (uint8, uint8, uint8) { + if len(h) > 0 && h[0] == '#' { + h = h[1:] + } + if len(h) == 3 { + h = h[:1] + h[:1] + h[1:2] + h[1:2] + h[2:] + h[2:] + } + if len(h) == 6 { + if rgb, err := strconv.ParseUint(string(h), 16, 32); err == nil { + return uint8(rgb >> 16), uint8((rgb >> 8) & 0xFF), uint8(rgb & 0xFF) + } + } + return 0, 0, 0 +}