From e80e039c1f60d3926b8f21c02254cfbf5a1f2987 Mon Sep 17 00:00:00 2001 From: Erik Garrison Date: Wed, 29 Nov 2023 15:39:37 -0600 Subject: [PATCH] make svg drawing work with colors --- src/algorithms/atomic_image.cpp | 19 +++++++++++++++++++ src/algorithms/atomic_image.hpp | 4 ++++ src/algorithms/draw.cpp | 15 +++++++++++---- src/algorithms/draw.hpp | 4 +++- src/subcommand/draw_main.cpp | 6 +++--- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/algorithms/atomic_image.cpp b/src/algorithms/atomic_image.cpp index 7f0a836b8..870e2759e 100644 --- a/src/algorithms/atomic_image.cpp +++ b/src/algorithms/atomic_image.cpp @@ -84,6 +84,25 @@ color_t mix(const color_t& a, const color_t& b, const double& f) { return out; } +std::string to_hex(const color_t& c) { + std::stringstream ss; + ss << "#"; + ss << std::hex << std::setfill('0') << std::setw(2) << (int)c.c.r; + ss << std::hex << std::setfill('0') << std::setw(2) << (int)c.c.g; + ss << std::hex << std::setfill('0') << std::setw(2) << (int)c.c.b; + return ss.str(); +} + +std::string to_rgba(const color_t& c) { + std::stringstream ss; + ss << "rgba("; + ss << (int)c.c.r << ","; + ss << (int)c.c.g << ","; + ss << (int)c.c.b << ","; + ss << (int)c.c.a << ")"; + return ss.str(); +} + // helpers double u_ipart(double x) { return std::floor(x); } diff --git a/src/algorithms/atomic_image.hpp b/src/algorithms/atomic_image.hpp index a9b380397..05472902f 100644 --- a/src/algorithms/atomic_image.hpp +++ b/src/algorithms/atomic_image.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "picosha2.h" namespace odgi { @@ -92,6 +93,9 @@ color_t darkest(const color_t& a, const color_t& b); color_t layer(const color_t& a, const color_t& b, const double& f); color_t mix(const color_t& a, const color_t& b, const double& f); +std::string to_hex(const color_t& c); +std::string to_rgba(const color_t& c); + const color_t COLOR_BLACK = { 0xff000000 }; const color_t COLOR_LIGHTGRAY = { 0xffD3D3D3 }; const color_t COLOR_WHITE = { 0xffffffff }; diff --git a/src/algorithms/draw.cpp b/src/algorithms/draw.cpp index 7d86d3b75..7d606c8f2 100644 --- a/src/algorithms/draw.cpp +++ b/src/algorithms/draw.cpp @@ -104,7 +104,9 @@ void draw_svg(std::ostream &out, const std::vector &Y, const PathHandleGraph &graph, const double& scale, - const double& border) { + const double& border, + const double& line_width, + std::vector& node_id_to_color) { std::vector> weak_components; coord_range_2d_t rendered_range; @@ -124,9 +126,10 @@ void draw_svg(std::ostream &out, << "viewBox=\"" << viewbox_x1 << " " << viewbox_y1 << " " << width << " " << height << "\"" << " xmlns=\"http://www.w3.org/2000/svg\">" - << "" + // interferes with the styling of the lines + //<< "" << std::endl; auto range_itr = component_ranges.begin(); @@ -134,8 +137,10 @@ void draw_svg(std::ostream &out, auto& range = *range_itr++; auto& x_off = range.x_offset; auto& y_off = range.y_offset; + //const algorithms::color_t node_color = !node_id_to_color.empty() ? node_id_to_color[graph.get_id(handle)] : COLOR_BLACK; for (auto& handle : component) { uint64_t a = 2 * number_bool_packing::unpack_number(handle); + algorithms::color_t color = node_id_to_color.empty() ? COLOR_BLACK : node_id_to_color[graph.get_id(handle)]; out << "" << std::endl; diff --git a/src/algorithms/draw.hpp b/src/algorithms/draw.hpp index 8a1f1be50..6315e90f9 100644 --- a/src/algorithms/draw.hpp +++ b/src/algorithms/draw.hpp @@ -69,7 +69,9 @@ void draw_svg(std::ostream &out, const std::vector &Y, const PathHandleGraph &graph, const double& scale, - const double& border); + const double& border, + const double& line_width, + std::vector& node_id_to_color); std::vector rasterize(const std::vector &X, const std::vector &Y, diff --git a/src/subcommand/draw_main.cpp b/src/subcommand/draw_main.cpp index 12edf2e60..620e69d21 100644 --- a/src/subcommand/draw_main.cpp +++ b/src/subcommand/draw_main.cpp @@ -41,7 +41,7 @@ int main_draw(int argc, char **argv) { args::ValueFlag png_height(visualizations_opts, "FILE", "Height of PNG rendering (default: 1000).", {'H', "png-height"}); args::ValueFlag png_border(visualizations_opts, "FILE", "Size of PNG border in bp (default: 10).", {'E', "png-border"}); args::Flag color_paths(visualizations_opts, "color-paths", "Color paths (in PNG output).", {'C', "color-paths"}); - args::ValueFlag render_scale(visualizations_opts, "N", "Image scaling (default 1.0).", {'R', "scale"}); + args::ValueFlag render_scale(visualizations_opts, "N", "Image scaling (default 0.001).", {'R', "scale"}); args::ValueFlag render_border(visualizations_opts, "N", "Image border (in approximate bp) (default 100.0).", {'B', "border"}); args::ValueFlag png_line_width(visualizations_opts, "N", "Line width (in approximate bp) (default 0.0).", {'w', "line-width"}); //args::ValueFlag png_line_overlay(parser, "N", "line width (in approximate bp) (default 10.0)", {'O', "line-overlay"}); @@ -174,7 +174,7 @@ int main_draw(int argc, char **argv) { const double _png_line_width = png_line_width ? args::get(png_line_width) : 0; const bool _color_paths = args::get(color_paths); const double _png_path_line_spacing = png_path_line_spacing ? args::get(png_path_line_spacing) : 0.0; - const double svg_scale = !render_scale ? 1.0 : args::get(render_scale); + const double svg_scale = !render_scale ? 0.01 : args::get(render_scale); size_t max_node_depth = 0; graph.for_each_handle( [&](const handle_t& h) { @@ -216,7 +216,7 @@ int main_draw(int argc, char **argv) { // todo could be done with callbacks std::vector X = layout.get_X(); std::vector Y = layout.get_Y(); - algorithms::draw_svg(f, X, Y, graph, svg_scale, border_bp); + algorithms::draw_svg(f, X, Y, graph, svg_scale, border_bp, _png_line_width, node_id_to_color); f.close(); }