Skip to content

Commit

Permalink
validate na_colour input #69
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Oct 12, 2020
1 parent 15b2d79 commit d5e6a15
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions inst/include/colourvalues/colours/generate_colours.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
namespace colourvalues {
namespace generate_colours {

inline std::string validate_na_colour( std::string na_colour, bool& include_alpha ) {
if( include_alpha && na_colour.length() == 9 ) {
return na_colour;
} else if ( include_alpha && na_colour.length() == 7 ) {
return na_colour + "FF";
} else if ( !include_alpha && na_colour.length() == 7 ) {
return na_colour;
} else if ( !include_alpha && na_colour.length() == 9 ) {
na_colour.pop_back();
na_colour.pop_back();
return na_colour;
}
Rcpp::stop("colourvalues - unknown na_colour hex string");
}

inline Rcpp::StringVector colour_values_to_hex(
Rcpp::NumericVector& x,
Rcpp::NumericVector& red,
Expand All @@ -27,7 +42,8 @@ namespace generate_colours {

R_xlen_t n = x.size();
double colours = red.size();
na_colour = na_colour.length() == 9 ? na_colour : na_colour + "FF";
//na_colour = na_colour.length() == 9 ? na_colour : na_colour + "FF";
na_colour = validate_na_colour( na_colour, include_alpha );

colourvalues::scale::rescale( x );
Rcpp::StringVector hex_strings( n );
Expand Down Expand Up @@ -95,7 +111,9 @@ namespace generate_colours {

R_xlen_t n = x.size();
double colours = red.size();
na_colour = na_colour.length() == 9 ? na_colour : na_colour + "FF";
//na_colour = na_colour.length() == 9 ? na_colour : na_colour + "FF";
na_colour = validate_na_colour( na_colour, include_alpha );


colourvalues::scale::rescale( x );
int cols = include_alpha ? 4 : 3;
Expand Down Expand Up @@ -168,7 +186,8 @@ namespace generate_colours {

R_xlen_t n = x.size();
double colours = red.size();
na_colour = na_colour.length() == 9 ? na_colour : na_colour + "FF";
//na_colour = na_colour.length() == 9 ? na_colour : na_colour + "FF";
na_colour = validate_na_colour( na_colour, include_alpha );

colourvalues::scale::rescale( x );
int cols = include_alpha ? 4 : 3;
Expand Down

0 comments on commit d5e6a15

Please sign in to comment.