Skip to content

Commit

Permalink
Merge pull request #152 from godardma/colormaps
Browse files Browse the repository at this point in the history
changed Color and added Colormaps
  • Loading branch information
SimonRohou authored Dec 5, 2024
2 parents 7ecc7cf + 567f03d commit e782e7f
Show file tree
Hide file tree
Showing 16 changed files with 727 additions and 95 deletions.
30 changes: 28 additions & 2 deletions examples/00_graphics/graphic_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
fig1.set_window_properties([50,50],[500,500]) # position, window size
fig1.set_axes(axis(0,[-10,10]), axis(1,[-10,10])) # (axis_id,[range_of_values_on_this_axis])
fig1.draw_box([[-1,1],[-1,1]],[Color.green(),Color.red(0.2)]) # drawing a green box with red opacity values inside
fig1.draw_circle([1,1],0.5,Color(255,155,5)) # drawing a circle at (1,1) of radius 0.5 with a custom RGB color
fig1.draw_circle([1,1],0.5,Color([255,155,5])) # drawing a circle at (1,1) of radius 0.5 with a custom RGB color
fig1.draw_ring([1,1],[4,6],Color.red()) # drawing a ring at (1,1) of radius [4,6] with a predefined red color

fig2 = Figure2D("My figure 2", GraphicOutput.VIBES | GraphicOutput.IPE)
Expand All @@ -44,5 +44,31 @@
fig2.draw_polygone([[2,4.5],[4,4.5],[4.2,3.5],[3.5,3]], [Color.none(),Color.green(0.5)])
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)])

# Colors
# predefined colors without and with opacity
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")])
# HTML color without and with opacity
fig2.draw_box([[2.4,2.9],[2.4,2.9]],[Color("#da3907"),Color("#da390755")])
# HSV color without and with opacity
fig2.draw_box([[2.6,3.1],[2.6,3.1]],[Color([108,90,78],Model.HSV),Color([108,90,78,20],Model.HSV)])
# RGB color auto cast from list without and with opacity
fig2.draw_box([[2.,2.3],[2.6,2.9]],[[255,0,255],[255,0,255,100]])

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])

cmap_haxby=ColorMap.haxby()
cmap_default=ColorMap.basic()
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
57 changes: 45 additions & 12 deletions python/src/graphics/styles/codac2_py_Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,60 @@ using namespace codac2;
namespace py = pybind11;
using namespace pybind11::literals;


void export_Color(py::module& m)
{

py::enum_<Model>(m, "Model")
.value("RGB", Model::RGB)
.value("HSV", Model::HSV)
;

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

.def(py::init<>(),COLOR_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(py::init<const std::array<float,3>&,Model>(),
COLOR_COLOR_CONST_ARRAY_FLOAT3_REF_MODEL,
"xyz"_a, "m_"_a=Model::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<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<float,4>&,Model>(),
COLOR_COLOR_CONST_ARRAY_FLOAT4_REF_MODEL,
"xyza"_a, "m_"_a=Model::RGB)

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

.def("model", &Color::model,
CONST_MODEL_REF_COLOR_MODEL_CONST)


// Other formats

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

.def("vec", &Color::vec,
VECTOR_COLOR_VEC_CONST)

// Conversions

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

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

// Overload flux operator

.def("__str__", [](const Color& c) {
std::ostringstream oss;
oss << c;
return oss.str();
})

// Predefined colors

.def_static("none", &Color::none,
Expand Down Expand Up @@ -79,4 +110,6 @@ void export_Color(py::module& m)
STATIC_COLOR_COLOR_DARK_GRAY_FLOAT,
"alpha"_a=1.)
;

py::implicitly_convertible<py::list, Color>();
}
55 changes: 55 additions & 0 deletions python/src/graphics/styles/codac2_py_ColorMap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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(py::init<Model>(),
COLORMAP_COLORMAP_MODEL,
"m"_a=Model::RGB)

.def("model", &ColorMap::model,
CONST_MODEL_REF_COLORMAP_MODEL_CONST)

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

// Predifined color maps

.def_static("haxby", &ColorMap::haxby,
STATIC_COLORMAP_COLORMAP_HAXBY)

.def_static("basic", &ColorMap::basic,
STATIC_COLORMAP_COLORMAP_BASIC)

.def_static("blue_tube", &ColorMap::blue_tube,
STATIC_COLORMAP_COLORMAP_BLUE_TUBE)

.def_static("red_tube", &ColorMap::red_tube,
STATIC_COLORMAP_COLORMAP_RED_TUBE)

.def_static("rainbow", &ColorMap::rainbow,
STATIC_COLORMAP_COLORMAP_RAINBOW)

;
}
57 changes: 35 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 @@ -75,18 +75,28 @@ void Figure2D_IPE::center_viewbox([[maybe_unused]] const Vector& c, [[maybe_unus
assert(r.min_coeff() > 0.);
}

std::string ipe_str(const Color& c)
{
return " codac_color_" + c.hex_str().substr(1);
}

int ipe_opacity(const Color& c)
{
return (int)(10.*round(10.*(c.model()==Model::RGB ? (c[3]/255.):(c[3]/100.))));
}

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(ipe_str(s.stroke_color), s.stroke_color);
_colors.emplace(ipe_str(s.fill_color), 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_" << ipe_str(s.stroke_color) << "\" \n \
fill=\"codac_color_" << ipe_str(s.fill_color) << "\" \n \
opacity=\"" << ipe_opacity(s.fill_color) << "%\" \n \
stroke-opacity=\"" << ipe_opacity(s.stroke_color) << "%\" \n \
pen=\"heavier\"";
if (tip)
_f_temp_content << "\n \
Expand All @@ -97,15 +107,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(ipe_str(s.stroke_color), s.stroke_color);
_colors.emplace(ipe_str(s.fill_color), 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_" << ipe_str(s.stroke_color) << "\" \n \
fill=\"codac_color_" << ipe_str(s.fill_color) << "\" \n \
opacity=\"" << ipe_opacity(s.fill_color) << "%\" \n \
stroke-opacity=\"" << ipe_opacity(s.stroke_color) << "%\" \n \
pen=\"heavier\" \n \
matrix=";

Expand All @@ -118,17 +128,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(ipe_str(s.stroke_color), s.stroke_color);
_colors.emplace(ipe_str(s.fill_color), 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_" << ipe_str(s.stroke_color) << "\" \n \
fill=\"codac_color_" << ipe_str(s.fill_color) << "\" \n \
opacity=\"" << ipe_opacity(s.fill_color) << "%\" \n \
stroke-opacity=\"" << ipe_opacity(s.stroke_color) << "%\" \n \
size=\"normal\"\n/>";
}

Expand Down Expand Up @@ -543,9 +553,12 @@ 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)
{
Color c_rgb = c.rgb();
_f << "<color name=\"codac_color_" << k << "\" "
<< "value=\"" << (float) (c_rgb[0]/255.) << " " <<(float) (c_rgb[1]/255.) << " " <<(float) (c_rgb[2]/255.) << "\" /> \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

0 comments on commit e782e7f

Please sign in to comment.