Commit e1163e09 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

move the color parsing code into color_reader

parent 82b53a66
Loading
Loading
Loading
Loading

core/color_reader.cc

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

namespace fviz {

ReturnCode color_read(
    const Environment& env,
    const Expr* expr,
    Color* value) {
  if (!expr_is_value(expr)) {
    return errorf(
        ERROR,
        "argument error to <color>; expected a value, got: {}",
        expr_inspect(expr));
  }

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

  return error(ERROR, "invalid color");
}

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

  *var = c;
  return OK;
}

} // namespace fviz

core/color_reader.h

0 → 100644
+33 −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 "environment.h"
#include "graphics/color.h"
#include "return_code.h"
#include "sexpr.h"

namespace fviz {

ReturnCode color_read(
    const Environment& env,
    const Expr* expr,
    Color* value);

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

} // namespace fviz
+4 −3
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
 */
#include "environment.h"
#include "core/format.h"
#include "color_reader.h"
#include "sexpr_conv.h"
#include "sexpr_util.h"
#include "graphics/font_lookup.h"
@@ -68,15 +69,15 @@ ReturnCode environment_set(Environment* env, const Expr* expr) {
  }

  if (expr_is_value(args[0], "background-color")) {
    return expr_to_color(args[1], &env->background_color);
    return color_read(*env, args[1], &env->background_color);
  }

  if (expr_is_value(args[0], "foreground-color")) {
    return expr_to_color(args[1], &env->foreground_color);
    return color_read(*env, args[1], &env->foreground_color);
  }

  if (expr_is_value(args[0], "text-color")) {
    return expr_to_color(args[1], &env->text_color);
    return color_read(*env, args[1], &env->text_color);
  }

  if (expr_is_value(args[0], "font")) {
+0 −32
Original line number Diff line number Diff line
@@ -175,38 +175,6 @@ ReturnCode expr_to_measures(
  return expr_tov<Measure>(expr, bind(&expr_to_measure, _1, _2), measures);
}

ReturnCode expr_to_color(
    const Expr* expr,
    Color* value) {
  if (!expr_is_value(expr)) {
    return errorf(
        ERROR,
        "argument error; expected a value, got: {}",
        "..."); // FIXME
  }

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

  return error(ERROR, "invalid color");
}

ReturnCode expr_to_color_opt(
    const Expr* expr,
    std::optional<Color>* var) {
  Color c;
  if (auto rc = expr_to_color(expr, &c); !rc) {
    return rc;
  }

  *var = c;
  return OK;
}

ReturnCode expr_to_stroke_style(
    const Expr* expr,
    StrokeStyle* style) {
+0 −9
Original line number Diff line number Diff line
@@ -14,7 +14,6 @@
#pragma once
#include "sexpr.h"
#include "graphics/brush.h"
#include "graphics/color.h"
#include "graphics/measure.h"

namespace fviz {
@@ -69,14 +68,6 @@ ReturnCode expr_to_measures(
    const Expr* expr,
    std::vector<Measure>* measures);

ReturnCode expr_to_color(
    const Expr* expr,
    Color* value);

ReturnCode expr_to_color_opt(
    const Expr* expr,
    std::optional<Color>* var);

ReturnCode expr_to_stroke_style(
    const Expr* expr,
    StrokeStyle* style);
Loading