From c0eaae200e4c6cfdef5a0a298921db02cba56a92 Mon Sep 17 00:00:00 2001 From: Lukas Dietrich Date: Sat, 27 Feb 2016 13:59:11 +0100 Subject: [PATCH] Add ForegroundColor for labels --- models/issue.go | 22 ++++++++++++++++++++++ templates/repo/issue/labels.tmpl | 2 +- templates/repo/issue/list.tmpl | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/models/issue.go b/models/issue.go index 62db35d48..c9883a25e 100644 --- a/models/issue.go +++ b/models/issue.go @@ -8,10 +8,12 @@ import ( "bytes" "errors" "fmt" + "html/template" "io" "mime/multipart" "os" "path" + "strconv" "strings" "time" @@ -958,6 +960,26 @@ func (m *Label) CalOpenIssues() { m.NumOpenIssues = m.NumIssues - m.NumClosedIssues } +// ForegroundColor calculates the text color for labels based +// on their background color +func (l *Label) ForegroundColor() template.CSS { + if strings.HasPrefix(l.Color, "#") { + if color, err := strconv.ParseUint(l.Color[1:], 16, 64); err == nil { + r := float32(0xFF & (color >> 16)) + g := float32(0xFF & (color >> 8)) + b := float32(0xFF & color) + luminance := (0.2126*r + 0.7152*g + 0.0722*b) / 255 + + if luminance < 0.5 { + return template.CSS("rgba(255,255,255,.8)") + } + } + } + + // default to black + return template.CSS("rgba(0,0,0,.8)") +} + // NewLabel creates new label of repository. func NewLabel(l *Label) error { _, err := x.Insert(l) diff --git a/templates/repo/issue/labels.tmpl b/templates/repo/issue/labels.tmpl index d7f758ae6..7fef4390c 100644 --- a/templates/repo/issue/labels.tmpl +++ b/templates/repo/issue/labels.tmpl @@ -39,7 +39,7 @@
{{range .Labels}}
  • -
    {{.Name}}
    +
    {{.Name}}
    {{if $.IsRepositoryAdmin}} {{$.i18n.Tr "repo.issues.label_delete"}} {{$.i18n.Tr "repo.issues.label_edit"}} diff --git a/templates/repo/issue/list.tmpl b/templates/repo/issue/list.tmpl index 66bd2ae29..e9a833925 100644 --- a/templates/repo/issue/list.tmpl +++ b/templates/repo/issue/list.tmpl @@ -105,7 +105,7 @@ {{.Name}} {{range .Labels}} - {{.Name}} + {{.Name}} {{end}} {{if .NumComments}}