Commit 76523be1 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add the global 'color-palette' option

parent 7eb069e2
Loading
Loading
Loading
Loading

core/color_palette.cc

0 → 100644
+71 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "fviz" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * 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.
 */
#include "color_palette.h"

namespace fviz {

const std::unordered_map<
    std::string_view,
    std::vector<std::string_view>>
    COLOR_PALETTES = {
  {
    "fnord",
    {
      "#3d96ae",
      "#4572a7",
      "#aa4643",
      "#db843d",
      "#89a54e",
      "#80699b",
    }
  },
};

ReturnCode color_palette_default(const std::string& name, ColorPalette* colors) {
  auto iter = COLOR_PALETTES.find(name);
  if (iter == COLOR_PALETTES.end()) {
    std::string valid_values;

    for (const auto& v : COLOR_PALETTES) {
      valid_values += fmt::format(
          "  - {}\n",
          std::string(v.first.data(), v.first.size()));
    }

    return errorf(
        ERROR,
        "invalid value to <color-palette-name>; got '{}' but expected one of:\n{}",
        name,
        valid_values);
  }

  colors->clear();

  for (const auto& v : iter->second) {
    Color c;
    c.parse(std::string(v.data(), v.length()));
    colors->push_back(c);
  }

  return OK;
}

ColorPalette color_palette_default() {
  ColorPalette c;
  color_palette_default("fnord", &c);
  return c;
}

} // namespace fviz
+6 −23
Original line number Diff line number Diff line
@@ -12,34 +12,17 @@
 * limitations under the License.
 */
#pragma once
#include <stdlib.h>
#include "graphics/color.h"
#include "return_code.h"

namespace fviz {
#include <vector>

class ColorScheme {
public:
namespace fviz {

  ColorScheme(
      const std::vector<std::string>& colors = std::vector<std::string>{
          "#4572a7",
          "#aa4643",
          "#89a54e",
          "#80699b",
          "#3d96ae",
          "#db843d",
          }) :
          colors_(colors) {}
using ColorPalette = std::vector<Color>;

  Color get(size_t seq) const {
    Color c;
    c.parse(colors_[seq % colors_.size()]);
    return c;
  }
ColorPalette color_palette_default();

protected:
  std::vector<std::string> colors_;
};
ReturnCode color_palette_default(const std::string& name, ColorPalette* colors);

} // namespace fviz
+50 −0
Original line number Diff line number Diff line
@@ -32,6 +32,15 @@ ReturnCode color_read(
        return OK;
      }
    }

    // color palette index
    if (StringUtil::isDigitString(value)) {
      if (!env.color_palette.empty()) {
        *color = env.color_palette[std::stoul(value) % env.color_palette.size()];
      }

      return OK;
    }
  }

  if (expr_is_list(expr)) {
@@ -80,12 +89,23 @@ ReturnCode color_read_string(
    const Environment& env,
    const std::string& value,
    Color* color) {

  // hex code
  if (StringUtil::beginsWith(value, "#")) {
    if (color->parse(value)) {
      return OK;
    }
  }

  // color palette index
  if (StringUtil::isDigitString(value)) {
    if (!env.color_palette.empty()) {
      *color = env.color_palette[std::stoul(value) % env.color_palette.size()];
    }

    return OK;
  }

  return errorf(
      ERROR,
      "invalid color; expected hex color code or palette index, got: '{}'",
@@ -126,5 +146,35 @@ ReturnCode color_map_read(
      expr_inspect(expr));
}

ReturnCode color_palette_read(
    const Environment& env,
    const Expr* expr,
    ColorPalette* color_palette) {
  color_palette->clear();

  if (expr_is_value(expr)) {
    return color_palette_default(expr_get_value(expr), color_palette);
  }

  if (expr_is_list(expr)) {
    Color c;
    for (auto arg = expr_get_list(expr); arg; arg = expr_next(arg)) {
      if (auto rc = color_read(env, arg, &c); !rc) {
        return rc;
      }

      color_palette->push_back(c);
    }

    return OK;
  }

  return errorf(
      ERROR,
      "invalid color palette; expected color palette name or color list, "
      "got: '{}'",
      expr_inspect(expr));
}

} // namespace fviz
+5 −0
Original line number Diff line number Diff line
@@ -40,5 +40,10 @@ ReturnCode color_map_read(
    const Expr* expr,
    ColorMap* color_map);

ReturnCode color_palette_read(
    const Environment& env,
    const Expr* expr,
    ColorPalette* color_palette);

} // namespace fviz
+6 −1
Original line number Diff line number Diff line
@@ -32,7 +32,8 @@ Environment::Environment() :
    background_color(Color::fromRGB(1,1,1)),
    foreground_color(Color::fromRGB(0,0,0)),
    text_color(Color::fromRGB(0,0,0)),
    font_size(from_pt(11)) {}
    font_size(from_pt(11)),
    color_palette(color_palette_default()) {}

ReturnCode environment_setup_defaults(Environment* env) {
  if (env->font_defaults) {
@@ -92,6 +93,10 @@ ReturnCode environment_set(Environment* env, const Expr* expr) {
    return expr_to_measure(args[1], &env->font_size);
  }

  if (expr_is_value(args[0], "color-palette")) {
    return color_palette_read(*env, args[1], &env->color_palette);
  }

  if (expr_is_value(args[0], "margin-top")) {
    return expr_to_measure(args[1], &env->margins[0]);
  }
Loading