Skip to content

Commit

Permalink
Update dominant color json response to return "white" or "black" stri…
Browse files Browse the repository at this point in the history
…ng instead of boolean
  • Loading branch information
zembrodt committed Nov 28, 2021
1 parent 1868d39 commit 8b926ac
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
16 changes: 11 additions & 5 deletions controller/color_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ const (

urlKey = "url"

barcodeColorThreshold = 186
barCodeColorThreshold = 186
)

var validDomains = map[string]bool {
"i.scdn.co": true,
func validDomains() map[string]bool {
return map[string]bool{
"i.scdn.co": true,
}
}

func (c *MusicAPIController) createColorHandlers() {
Expand Down Expand Up @@ -51,7 +53,7 @@ func (c *MusicAPIController) getDominateColor(w http.ResponseWriter, r *http.Req
return
}
// Check if the domain for this image is an accepted one
if !validDomains[strings.ToLower(coverArtUrl.Hostname())] {
if !validDomains()[strings.ToLower(coverArtUrl.Hostname())] {
respondWithError(w, http.StatusBadRequest, "The provided domain in the URL is invalid")
return
}
Expand Down Expand Up @@ -85,7 +87,11 @@ func (c *MusicAPIController) getDominateColor(w http.ResponseWriter, r *http.Req
rgb := dominantcolor.Find(img)
colorResponse.Color = dominantcolor.Hex(rgb)
// See https://stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color
colorResponse.UseBlackBarcodeColor = float32(rgb.R) * 0.299 + float32(rgb.G) * 0.587 + float32(rgb.B) * 0.114 > barcodeColorThreshold
if float32(rgb.R) * 0.299 + float32(rgb.G) * 0.587 + float32(rgb.B) * 0.114 > barCodeColorThreshold {
colorResponse.BarCodeColor = "black"
} else {
colorResponse.BarCodeColor = "white"
}

respondWithJSON(w, http.StatusOK, colorResponse)
}
4 changes: 2 additions & 2 deletions model/color_models.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package model

type ColorResponse struct {
Color string `json:"color" example:"#1500ff"`
UseBlackBarcodeColor bool `json:"useBlackBarcodeColor" example:"true"`
Color string `json:"color" example:"#1500ff"`
BarCodeColor string `json:"barCodeColor" example:"white,black"`
}
2 changes: 1 addition & 1 deletion properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package musicapi

const (
Name = "Music Display API"
Version = "0.0.1-SNAPSHOT"
Version = "0.0.2"
APIRoot = "/api"
)

0 comments on commit 8b926ac

Please sign in to comment.