Skip to content

Commit

Permalink
Merge pull request #245 from brharrington/palette-list
Browse files Browse the repository at this point in the history
support user defined color palette
  • Loading branch information
brharrington committed Nov 7, 2015
2 parents 2bd623d + d101ee5 commit 92b4bc5
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package com.netflix.atlas.chart.model

import java.awt.Color
import java.io.FileNotFoundException
import java.util.concurrent.ConcurrentHashMap

import com.netflix.atlas.chart.Colors
import com.netflix.atlas.core.util.Strings

case class Palette(name: String, colors: Int => Color) {
def withAlpha(alpha: Int): Palette = {
Expand Down Expand Up @@ -74,17 +76,47 @@ object Palette {

private val palettes = new ConcurrentHashMap[String, Palette]

/**
* Creates a palette instance from a description string. The description can be an explicit
* list of colors or the name of a palette file in the classpath. An explicit list is specified
* with a prefix of 'colors:' followed by a comma separated list of color values. For example:
*
* ```
* colors:f00,00ff00,000000ff
* ```
*
* The color values will be parsed using [[com.netflix.atlas.core.util.Strings.parseColor]].
* Otherwise the description will be used to find a palette file in the classpath named
* 'palettes/${desc}_palette.txt' that has one color per line.
*/
def create(desc: String): Palette = {
if (desc.startsWith("colors:"))
fromArray("colors", desc.substring("colors:".length).split(",").map(Strings.parseColor))
else
fromResource(desc)
}

def fromArray(name: String, colors: Array[Color]): Palette = {
Palette(name, i => colors(math.abs(i) % colors.length))
}

/**
* Create a palette from a file in the classpath named 'palettes/${name}_palette.txt'. The
* file should have one color per line in a format supported by
* [[com.netflix.atlas.core.util.Strings.parseColor]].
*/
def fromResource(name: String): Palette = {
palettes.computeIfAbsent(name, loadFromResource)
}

private def loadFromResource(name: String): Palette = {
val colors = Colors.load(s"palettes/${name}_palette.txt").toArray
Palette.fromArray(name, colors)
try {
val colors = Colors.load(s"palettes/${name}_palette.txt").toArray
Palette.fromArray(name, colors)
} catch {
case _: FileNotFoundException =>
throw new IllegalArgumentException(s"invalid palette name: '$name'")
}
}

def singleColor(c: Color): Palette = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2015 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.atlas.chart.model

import java.awt.Color

import org.scalatest.FunSuite


class PaletteSuite extends FunSuite {

test("colors:") {
intercept[IllegalArgumentException] {
Palette.create("colors:")
}
}

test("colors:foo") {
intercept[IllegalArgumentException] {
Palette.create("colors:foo")
}
}

test("colors:f00") {
val p = Palette.create("colors:f00")
assert(p.colors(0) === Color.RED)
}

test("colors:f00,00ff00") {
val p = Palette.create("colors:f00,00ff00")
assert(p.colors(0) === Color.RED)
assert(p.colors(1) === Color.GREEN)
}

test("colors:f00,00ff00,ff0000ff") {
val p = Palette.create("colors:f00,00ff00,ff0000ff")
assert(p.colors(0) === Color.RED)
assert(p.colors(1) === Color.GREEN)
assert(p.colors(2) === Color.BLUE)
}

test("armytage") {
val p = Palette.create("armytage")
assert(p.colors(0) === new Color(0, 117, 220))
}

test("foo") {
intercept[IllegalArgumentException] {
Palette.create("foo")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class GraphRequestActor(registry: Registry) extends Actor with ActorLogging {
val plotExprs = request.exprs.groupBy(_.axis.getOrElse(0))
val multiY = plotExprs.size > 1

val palette = Palette.fromResource(request.flags.palette).iterator
val shiftPalette = Palette.fromResource("bw").iterator
val palette = Palette.create(request.flags.palette).iterator
val shiftPalette = Palette.create("bw").iterator

val plots = plotExprs.toList.sortWith(_._1 < _._1).map { case (yaxis, exprs) =>

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions atlas-webapi/src/test/resources/others.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@
/api/v1/graph?s=e-1d&e=2012-01-01T00:00&q=name,sps,:eq,nf.cluster,nccp-wii,:lt,:and,:sum,(,nf.cluster,),:by&no_tick_labels=1&axis_per_line=1
/api/v1/graph?s=e-1d&e=2012-01-01T00:00&q=name,sps,:eq,nf.cluster,nccp-wii,:lt,:and,:sum,(,nf.cluster,),:by&no_tick_labels.2=1&axis_per_line=1
/api/v1/graph?s=e-1d&e=2012-01-01T00:00&q=name,sps,:eq,nf.cluster,nccp-wii,:lt,:and,:sum,(,nf.cluster,),:by&no_tick_labels.2=1&axis_per_line=1&no_legend_stats=1

# Palettes
/api/v1/graph?s=e-1d&e=2012-01-01T00:00&q=name,sps,:eq,:sum,(,nf.cluster,),:by,:pct,$nf.cluster,:legend,:stack&palette=armytage
/api/v1/graph?s=e-1d&e=2012-01-01T00:00&q=name,sps,:eq,:sum,(,nf.cluster,),:by,:pct,$nf.cluster,:legend,:stack&palette=epic
/api/v1/graph?s=e-1d&e=2012-01-01T00:00&q=name,sps,:eq,:sum,(,nf.cluster,),:by,:pct,$nf.cluster,:legend,:stack&palette=bw
/api/v1/graph?s=e-1d&e=2012-01-01T00:00&q=name,sps,:eq,:sum,(,nf.cluster,),:by,:pct,$nf.cluster,:legend,:stack&palette=colors:1a9850,91cf60,d9ef8b,fee08b,fc8d59,d73027

0 comments on commit 92b4bc5

Please sign in to comment.