Skip to content

Commit

Permalink
Move calculating the Spotify bar code color to client-side
Browse files Browse the repository at this point in the history
  • Loading branch information
zembrodt committed Nov 29, 2021
1 parent 8b926ac commit 4e464cd
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 26 deletions.
22 changes: 3 additions & 19 deletions controller/color_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controller
import (
"bytes"
"github.com/cenkalti/dominantcolor"
"github.com/zembrodt/music-display-api/model"
"image"
_ "image/jpeg"
_ "image/png"
Expand All @@ -15,10 +14,7 @@ import (

const (
pathColor = "/color"

urlKey = "url"

barCodeColorThreshold = 186
urlKey = "url"
)

func validDomains() map[string]bool {
Expand All @@ -39,8 +35,6 @@ func (c *MusicAPIController) getDominateColor(w http.ResponseWriter, r *http.Req
}

encodedUrl := urls[0]
// TODO: get image from url

coverArtUrlRaw, err := url.QueryUnescape(encodedUrl)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Unable to unescape the provided cover art url: " + err.Error())
Expand Down Expand Up @@ -82,16 +76,6 @@ func (c *MusicAPIController) getDominateColor(w http.ResponseWriter, r *http.Req
return
}

// Get dominant color of the album art
var colorResponse model.ColorResponse
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
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)
// Return dominant color of the album art
respondWithJSON(w, http.StatusOK, dominantcolor.Hex(dominantcolor.Find(img)))
}
6 changes: 0 additions & 6 deletions model/color_models.go

This file was deleted.

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.2"
Version = "0.1.0"
APIRoot = "/api"
)

0 comments on commit 4e464cd

Please sign in to comment.