Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed Color and added Colormaps #152

Merged
merged 18 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion examples/00_graphics/graphic_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,23 @@
fig2.draw_polyline([[-0.8,0],[0,1.5]], 0.2, [Color.red(),Color.black(0.3)])
fig2.draw_ellipse([1,1],[0.5,2], 0.2, [Color.blue(),Color.blue(0.3)])
fig2.draw_point([2,2], [Color.red(),Color.yellow(0.5)])
fig2.draw_box([[2.4,2.9],[2.4,2.9]],[Color("#da3907"),Color("#da390755")])
fig2.draw_box([[2.4,2.9],[2.4,2.9]],[Color("#da3907"),Color("#da390755")])
godardma marked this conversation as resolved.
Show resolved Hide resolved
fig2.draw_box([[2.6,3.1],[2.6,3.1]],[Color(0.30,0.9,0.78,interpol_mode=InterpolMode.HSV),Color(0.30,0.9,0.78,0.2,InterpolMode.HSV)])

fig3 = Figure2D("ColorMap figure", GraphicOutput.VIBES | GraphicOutput.IPE)
fig3.set_axes(axis(0,[-1,21]), axis(1,[-5.5,0.5]))
fig3.set_window_properties([800,250],[500,500])

godardma marked this conversation as resolved.
Show resolved Hide resolved
cmap_haxby=ColorMap.HAXBY
cmap_default=ColorMap.DEFAULT
cmap_blue_tube=ColorMap.BLUE_TUBE
cmap_red_tube=ColorMap.RED_TUBE
cmap_rainbow=ColorMap.RAINBOW

for i in range (20):
ratio=i/20
fig3.draw_box([[i,i+1],[-1,0]],[Color.black(),cmap_haxby.color(ratio)])
fig3.draw_box([[i,i+1],[-2,-1]],[Color.black(),cmap_default.color(ratio)])
fig3.draw_box([[i,i+1],[-3,-2]],[Color.black(),cmap_blue_tube.color(ratio)])
fig3.draw_box([[i,i+1],[-4,-3]],[Color.black(),cmap_red_tube.color(ratio)])
fig3.draw_box([[i,i+1],[-5,-4]],[Color.black(),cmap_rainbow.color(ratio)])
1 change: 1 addition & 0 deletions python/src/graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
paver/codac2_py_drawwhilepaving.cpp

styles/codac2_py_Color.cpp
styles/codac2_py_ColorMap.cpp
styles/codac2_py_StyleProperties.cpp
)

Expand Down
2 changes: 2 additions & 0 deletions python/src/graphics/codac2_py_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void export_drawwhilepaving(py::module& m);

// styles
void export_Color(py::module& m);
void export_ColorMap(py::module& m);
void export_StyleProperties(py::module& m);


Expand All @@ -31,6 +32,7 @@ PYBIND11_MODULE(_graphics, m)

// styles
export_Color(m);
export_ColorMap(m);
export_StyleProperties(m);

// figures
Expand Down
79 changes: 68 additions & 11 deletions python/src/graphics/styles/codac2_py_Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,86 @@ using namespace codac2;
namespace py = pybind11;
using namespace pybind11::literals;


void export_Color(py::module& m)
{
py::enum_<InterpolMode>(m, "InterpolMode")
.value("RGB", InterpolMode::RGB)
.value("HSV", InterpolMode::HSV)
;

py::class_<Color> exported_color(m, "Color", COLOR_MAIN);
exported_color

.def_readwrite("r", &Color::r)
.def_readwrite("g", &Color::g)
.def_readwrite("b", &Color::b)
.def_readwrite("alpha", &Color::alpha)
.def_readwrite("hex_str", &Color::hex_str)
.def_readwrite("data", &Color::data,
ARRAY_FLOAT4_COLOR_DATA)

.def_readwrite("interpol_mode", &Color::interpol_mode,
INTERPOLMODE_COLOR_INTERPOL_MODE)

.def(py::init<>(),COLOR_COLOR)

.def(py::init<float,float,float,float,InterpolMode>(),
COLOR_COLOR_FLOAT_FLOAT_FLOAT_FLOAT_INTERPOLMODE,
"x1"_a, "x2"_a, "x3"_a, "alpha"_a, "interpol_mode"_a=InterpolMode::RGB)

.def(py::init<float,float,float,InterpolMode>(),
COLOR_COLOR_FLOAT_FLOAT_FLOAT_INTERPOLMODE,
"x1"_a, "x2"_a, "x3"_a, "interpol_mode"_a=InterpolMode::RGB)

.def(py::init<const std::array<float,3>&,InterpolMode>(),
COLOR_COLOR_CONST_ARRAY_FLOAT3_REF_INTERPOLMODE,
"xyz"_a, "interpol_mode"_a=InterpolMode::RGB)

.def(py::init<const std::array<float,4>&,InterpolMode>(),
COLOR_COLOR_CONST_ARRAY_FLOAT4_REF_INTERPOLMODE,
"xyza"_a, "interpol_mode"_a=InterpolMode::RGB)

.def(py::init<int,int,int,int,InterpolMode>(),
COLOR_COLOR_INT_INT_INT_INT_INTERPOLMODE,
"x1"_a, "x2"_a, "x3"_a, "alpha"_a, "interpol_mode"_a=InterpolMode::RGB)

.def(py::init<int,int,int,int>(),
COLOR_COLOR_INT_INT_INT_INT,
"r"_a, "g"_a, "b"_a, "alpha"_a=255)
.def(py::init<int,int,int,InterpolMode>(),
COLOR_COLOR_INT_INT_INT_INTERPOLMODE,
"x1"_a, "x2"_a, "x3"_a, "interpol_mode"_a=InterpolMode::RGB)

.def(py::init<float,float,float,float>(),
COLOR_COLOR_FLOAT_FLOAT_FLOAT_FLOAT,
"r"_a, "g"_a, "b"_a, "alpha"_a=1.)
.def(py::init<const std::array<int,3>&,InterpolMode>(),
COLOR_COLOR_CONST_ARRAY_INT3_REF_INTERPOLMODE,
"xyz"_a, "interpol_mode"_a=InterpolMode::RGB)

.def(py::init<const std::array<int,4>&,InterpolMode>(),
COLOR_COLOR_CONST_ARRAY_INT4_REF_INTERPOLMODE,
"xyza"_a, "interpol_mode"_a=InterpolMode::RGB)

.def(py::init<const std::string&>(),
COLOR_COLOR_CONST_STRING_REF,
"hex_str"_a)

// Conversions

.def("toRGB", &Color::toRGB,
COLOR_COLOR_TORGB_CONST)

.def("toHSV", &Color::toHSV,
COLOR_COLOR_TOHSV_CONST)

// Properties

.def("hex_str", &Color::hex_str,
STRING_COLOR_HEX_STR_CONST)

.def("rgb", &Color::rgb,
ARRAY_INT3_COLOR_RGB_CONST)

.def("rgba", &Color::rgba,
ARRAY_INT3_COLOR_RGB_CONST)

.def("hsv", &Color::hsv,
ARRAY_INT3_COLOR_RGB_CONST)

.def("hsva", &Color::hsva,
ARRAY_INT3_COLOR_RGB_CONST)

// Predefined colors

.def_static("none", &Color::none,
Expand Down
57 changes: 57 additions & 0 deletions python/src/graphics/styles/codac2_py_ColorMap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Codac binding (core)
* ----------------------------------------------------------------------------
* \date 2024
* \author Simon Rohou, Maël Godard
* \copyright Copyright 2024 Codac Team
* \license GNU Lesser General Public License (LGPL)
*/

#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>
#include <codac2_ColorMap.h>
#include "codac2_py_ColorMap_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py):

using namespace std;
using namespace codac2;
namespace py = pybind11;
using namespace pybind11::literals;

void export_ColorMap(py::module& m)
{
py::class_<ColorMap> exported_colormap(m, "ColorMap", COLORMAP_MAIN);
exported_colormap

.def_readwrite("colormap", &ColorMap::colormap)

.def(py::init<>(),
COLORMAP_COLORMAP)

.def("add_color_point", &ColorMap::add_color_point,
VOID_COLORMAP_ADD_COLOR_POINT_COLOR_FLOAT,
"color"_a, "index"_a)

.def("color", &ColorMap::color,
COLOR_COLORMAP_COLOR_FLOAT_CONST,
"r"_a)

// Predifined color maps

.def_readonly_static("HAXBY", &ColorMap::HAXBY,
CONST_COLORMAP_COLORMAP_HAXBY)

.def_readonly_static("DEFAULT", &ColorMap::DEFAULT,
CONST_COLORMAP_COLORMAP_DEFAULT)

.def_readonly_static("BLUE_TUBE", &ColorMap::BLUE_TUBE,
CONST_COLORMAP_COLORMAP_BLUE_TUBE)

.def_readonly_static("RED_TUBE", &ColorMap::RED_TUBE,
CONST_COLORMAP_COLORMAP_RED_TUBE)

.def_readonly_static("RAINBOW", &ColorMap::RAINBOW,
CONST_COLORMAP_COLORMAP_RAINBOW)

;
}
48 changes: 26 additions & 22 deletions src/graphics/3rd/ipe/codac2_Figure2D_IPE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Figure2D_IPE::Figure2D_IPE(const Figure2D& fig)

for(const auto& ci : codac_colors)
// substr is needed to remove the "#" at the beginning of hex_str (deprecated by IPE)
_colors.emplace(ci.hex_str.substr(1), ci);
_colors.emplace(ci.hex_str().substr(1), ci);
}

Figure2D_IPE::~Figure2D_IPE()
Expand Down Expand Up @@ -78,15 +78,15 @@ void Figure2D_IPE::center_viewbox([[maybe_unused]] const Vector& c, [[maybe_unus
void Figure2D_IPE::begin_path(const StyleProperties& s, bool tip=false)
{
// substr is needed to remove the "#" at the beginning of hex_str (deprecated by IPE)
_colors.emplace(s.stroke_color.hex_str.substr(1), s.stroke_color);
_colors.emplace(s.fill_color.hex_str.substr(1), s.fill_color);
_colors.emplace(s.stroke_color.hex_str().substr(1), s.stroke_color);
godardma marked this conversation as resolved.
Show resolved Hide resolved
_colors.emplace(s.fill_color.hex_str().substr(1), s.fill_color);

_f_temp_content << "\n \
<path layer=\"alpha\" \n \
stroke=\"codac_color_" << s.stroke_color.hex_str.substr(1) << "\" \n \
fill=\"codac_color_" << s.fill_color.hex_str.substr(1) << "\" \n \
opacity=\"" << (int)(10*round(10.*s.fill_color.alpha)) << "%\" \n \
stroke-opacity=\"" << (int)(10*round(10.*s.stroke_color.alpha)) << "%\" \n \
stroke=\"codac_color_" << s.stroke_color.hex_str().substr(1) << "\" \n \
fill=\"codac_color_" << s.fill_color.hex_str().substr(1) << "\" \n \
opacity=\"" << (int)(10*round(10.*s.fill_color.data[3])) << "%\" \n \
stroke-opacity=\"" << (int)(10*round(10.*s.stroke_color.data[3])) << "%\" \n \
pen=\"heavier\"";
if (tip)
_f_temp_content << "\n \
Expand All @@ -97,15 +97,15 @@ void Figure2D_IPE::begin_path(const StyleProperties& s, bool tip=false)
void Figure2D_IPE::begin_path_with_matrix(const Vector& x, float length, const StyleProperties& s)
{
// substr is needed to remove the "#" at the beginning of hex_str (deprecated by IPE)
_colors.emplace(s.stroke_color.hex_str.substr(1), s.stroke_color);
_colors.emplace(s.fill_color.hex_str.substr(1), s.fill_color);
_colors.emplace(s.stroke_color.hex_str().substr(1), s.stroke_color);
_colors.emplace(s.fill_color.hex_str().substr(1), s.fill_color);

_f_temp_content << "\n \
<path layer=\"alpha\" \n \
stroke=\"codac_color_" << s.stroke_color.hex_str.substr(1) << "\" \n \
fill=\"codac_color_" << s.fill_color.hex_str.substr(1) << "\" \n \
opacity=\"" << (int)(10*round(10.*s.fill_color.alpha)) << "%\" \n \
stroke-opacity=\"" << (int)(10*round(10.*s.stroke_color.alpha)) << "%\" \n \
stroke=\"codac_color_" << s.stroke_color.hex_str().substr(1) << "\" \n \
fill=\"codac_color_" << s.fill_color.hex_str().substr(1) << "\" \n \
opacity=\"" << (int)(10*round(10.*s.fill_color.data[3])) << "%\" \n \
stroke-opacity=\"" << (int)(10*round(10.*s.stroke_color.data[3])) << "%\" \n \
pen=\"heavier\" \n \
matrix=";

Expand All @@ -118,17 +118,17 @@ void Figure2D_IPE::begin_path_with_matrix(const Vector& x, float length, const S
void Figure2D_IPE::draw_point(const Vector& c, const StyleProperties& s)
{
assert(_fig.size() <= c.size());
_colors.emplace(s.stroke_color.hex_str.substr(1), s.stroke_color);
_colors.emplace(s.fill_color.hex_str.substr(1), s.fill_color);
_colors.emplace(s.stroke_color.hex_str().substr(1), s.stroke_color);
_colors.emplace(s.fill_color.hex_str().substr(1), s.fill_color);

_f_temp_content << "\n \
<use layer=\"alpha\" \n \
name=\"mark/fdisk(sfx)\" \n \
pos=\"" << scale_x(c[i()]) << " " << scale_y(c[j()]) << "\" \n \
stroke=\"codac_color_" << s.stroke_color.hex_str.substr(1) << "\" \n \
fill=\"codac_color_" << s.fill_color.hex_str.substr(1) << "\" \n \
opacity=\"" << (int)(10*round(10.*s.fill_color.alpha)) << "%\" \n \
stroke-opacity=\"" << (int)(10*round(10.*s.stroke_color.alpha)) << "%\" \n \
stroke=\"codac_color_" << s.stroke_color.hex_str().substr(1) << "\" \n \
fill=\"codac_color_" << s.fill_color.hex_str().substr(1) << "\" \n \
opacity=\"" << (int)(10*round(10.*s.fill_color.data[3])) << "%\" \n \
stroke-opacity=\"" << (int)(10*round(10.*s.stroke_color.data[3])) << "%\" \n \
size=\"normal\"\n/>";
}

Expand Down Expand Up @@ -543,9 +543,13 @@ void Figure2D_IPE::print_header_page()
<arrowsize name=\"small\" value=\"5\"/> \n \
<arrowsize name=\"tiny\" value=\"3\"/> \n";

for(const auto& [k,c] : _colors)
_f << "<color name=\"codac_color_" << k << "\" "
<< "value=\"" << c.r << " " << c.g << " " << c.b << "\" /> \n";
for(auto& [k,c] : _colors)
{
if (c.interpol_mode == InterpolMode::HSV)
godardma marked this conversation as resolved.
Show resolved Hide resolved
c = c.toRGB();
_f << "<color name=\"codac_color_" << k << "\" "
<< "value=\"" << c.data[0] << " " << c.data[1] << " " << c.data[2] << "\" /> \n";
}

_f << "<dashstyle name=\"dash dot dotted\" value=\"[4 2 1 2 1 2] 0\"/> \n \
<dashstyle name=\"dash dotted\" value=\"[4 2 1 2] 0\"/> \n \
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/3rd/vibes/codac2_Figure2D_VIBes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,5 @@ void Figure2D_VIBes::draw_AUV(const Vector& x, float size, const StyleProperties

string Figure2D_VIBes::to_vibes_style(const StyleProperties& s)
{
return s.stroke_color.hex_str + "[" + s.fill_color.hex_str + "]";
return s.stroke_color.hex_str() + "[" + s.fill_color.hex_str() + "]";
}
2 changes: 2 additions & 0 deletions src/graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

${CMAKE_CURRENT_SOURCE_DIR}/styles/codac2_Color.cpp
${CMAKE_CURRENT_SOURCE_DIR}/styles/codac2_Color.h
${CMAKE_CURRENT_SOURCE_DIR}/styles/codac2_ColorMap.cpp
${CMAKE_CURRENT_SOURCE_DIR}/styles/codac2_ColorMap.h
${CMAKE_CURRENT_SOURCE_DIR}/styles/codac2_StyleProperties.cpp
${CMAKE_CURRENT_SOURCE_DIR}/styles/codac2_StyleProperties.h
)
Expand Down
Loading
Loading