Commit 16060cb7 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

improved color mapping

parent e1163e09
Loading
Loading
Loading
Loading

core/color_map.cc

0 → 100644
+19 −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_map.h"

namespace fviz {

} // namespace fviz

core/color_map.h

0 → 100644
+25 −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.
 */
#pragma once
#include "graphics/color.h"
#include "return_code.h"

namespace fviz {

using ColorMap = std::function<ReturnCode (double v, Color* c)>;

ColorMap color_map_raw(std::vector<Color> colors);

} // namespace fviz
+48 −5
Original line number Diff line number Diff line
@@ -11,14 +11,17 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "data.h"
#include "color_reader.h"

using namespace std::placeholders;

namespace fviz {

ReturnCode color_read(
    const Environment& env,
    const Expr* expr,
    Color* value) {
    Color* color) {
  if (!expr_is_value(expr)) {
    return errorf(
        ERROR,
@@ -28,26 +31,66 @@ ReturnCode color_read(

  const auto expr_value = expr_get_value(expr);
  if (StringUtil::beginsWith(expr_value, "#")) {
    if (value->parse(expr_value)) {
    if (color->parse(expr_value)) {
      return OK;
    }
  }

  return error(ERROR, "invalid color");
  return errorf(
      ERROR,
      "invalid color; expected hex color code, rgb(a) or palette index, got: '{}'",
      expr_inspect(expr));
}

ReturnCode color_read_string(
    const Environment& env,
    const std::string& value,
    Color* color) {
  if (StringUtil::beginsWith(value, "#")) {
    if (color->parse(value)) {
      return OK;
    }
  }

  return errorf(
      ERROR,
      "invalid color; expected hex color code or palette index, got: '{}'",
      value);
}

ReturnCode color_read_opt(
    const Environment& env,
    const Expr* expr,
    std::optional<Color>* var) {
    std::optional<Color>* color) {
  Color c;
  if (auto rc = color_read(env, expr, &c); !rc) {
    return rc;
  }

  *var = c;
  *color = c;
  return OK;
}

ReturnCode color_map_read(
    const Environment& env,
    const Expr* expr,
    ColorMap* color_map) {
  if (!expr || !expr_is_list(expr)) {
    return errorf(
        ERROR,
        "invalid argument to <color-map>; expected a list, but got: '{}'",
        expr_inspect(expr));
  }

  expr = expr_get_list(expr);

  return errorf(
      ERROR,
      "invalid value to <color-map>; got '{}', but expected one of: \n"
      "  - steps\n"
      "  - gradient\n",
      expr_inspect(expr));
}

} // namespace fviz
+13 −2
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
#pragma once
#include "environment.h"
#include "graphics/color.h"
#include "color_map.h"
#include "return_code.h"
#include "sexpr.h"

@@ -22,12 +23,22 @@ namespace fviz {
ReturnCode color_read(
    const Environment& env,
    const Expr* expr,
    Color* value);
    Color* color);

ReturnCode color_read_string(
    const Environment& env,
    const std::string& value,
    Color* color);

ReturnCode color_read_opt(
    const Environment& env,
    const Expr* expr,
    std::optional<Color>* var);
    std::optional<Color>* color);

ReturnCode color_map_read(
    const Environment& env,
    const Expr* expr,
    ColorMap* color_map);

} // namespace fviz
+0 −16
Original line number Diff line number Diff line
@@ -87,22 +87,6 @@ std::vector<DataGroup> series_group(const Series& data) {
  return groups;
}

std::vector<Color> groups_to_colors(
    size_t count,
    const std::vector<DataGroup>& groups,
    const ColorScheme& palette) {
  std::vector<Color> colors(count);
  for (size_t gi = 0; gi < groups.size(); ++gi) {
    for (auto i : groups[gi].index) {
      if (i < colors.size()) {
        colors[i] = palette.get(gi);
      }
    }
  }

  return colors;
}

ReturnCode data_load_strings_csv(
    const Expr* expr,
    std::vector<std::string>* values) {
Loading