Skip to content

Commit

Permalink
fix alpha-index issue and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Nov 14, 2019
1 parent e2f6e00 commit d9aa664
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 19 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: colourvalues
Type: Package
Title: Assigns Colours to Values
Version: 0.3.1
Date: 2019-11-11
Date: 2019-11-15
Authors@R: c(
person("David", "Cooley", ,"[email protected]", role = c("aut", "cre"))
)
Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ rcpp_convert_rgb_vec_to_hex <- function(rgb) {
.Call(`_colourvalues_rcpp_convert_rgb_vec_to_hex`, rgb)
}

rcpp_validate_alpha <- function(alpha, alpha_type) {
.Call(`_colourvalues_rcpp_validate_alpha`, alpha, alpha_type)
}

rcpp_get_format_type <- function(palette_type) {
.Call(`_colourvalues_rcpp_get_format_type`, palette_type)
}
Expand Down
11 changes: 3 additions & 8 deletions inst/include/colourvalues/alpha/alpha.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace alpha {
return ALPHA_UNKNOWN; // #nocov
}

inline Rcpp::NumericVector validate_alpha( Rcpp::NumericVector& a, int& alpha_type, int& x_size ) {
inline Rcpp::NumericVector validate_alpha( Rcpp::NumericVector& a, int& alpha_type ) {

if( a.size() < 1 ) {
Rcpp::stop("colourvalues - invalid alpha vector");
Expand All @@ -49,7 +49,7 @@ namespace alpha {
Rcpp::NumericVector alpha_full( 5, alpha[0] ); // initialise with 5 vals (so i can create a spline object);
return alpha_full;

} else if (alpha_type == ALPHA_VECTOR ) {
} else if ( alpha_type == ALPHA_VECTOR ) {

colourvalues::scale::rescale( alpha );
alpha = alpha * 255;
Expand All @@ -58,20 +58,15 @@ namespace alpha {
int n_alpha = alpha.size();

if ( n_alpha < 5 ) {
int to_fill = 5 - n_alpha;
double mean_alpha = Rcpp::mean( alpha );
Rcpp::NumericVector alpha_fill( 5 );
Rcpp::NumericVector alpha_fill( 5, mean_alpha );
int i = 0;
for ( i = 0; i < n_alpha; i++ ) {
alpha_fill[i] = alpha[i];
}
for ( i = 1; i <= to_fill; i++ ) {
alpha_fill[ n_alpha + to_fill ] = mean_alpha;
}
return alpha_fill;
}
return alpha;

}

Rcpp::NumericVector out;
Expand Down
10 changes: 5 additions & 5 deletions inst/include/colourvalues/colours/colours_hex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace colours_hex {

colourvalues::palette_utils::resolve_palette( palette, red, green, blue, alpha );

Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type, x_size );
Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type );

if ( n_summaries > 0 ) {
Rcpp::NumericVector summary = colourvalues::summary::numeric_summary( x, n_summaries );
Expand Down Expand Up @@ -147,7 +147,7 @@ inline SEXP colour_value_hex(
int x_size = x.size();
int alpha_type = colourvalues::alpha::make_alpha_type( alpha.size(), x_size, 0 );

Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type, x_size );
Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type );

Rcpp::NumericVector red(256);
Rcpp::NumericVector green(256);
Expand Down Expand Up @@ -247,7 +247,7 @@ inline SEXP colour_value_hex(
int x_size = x.size();
int alpha_type = colourvalues::alpha::make_alpha_type( alpha.size(), x_size, 0 );

Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type, x_size );
Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type );

Rcpp::NumericVector red(256);
Rcpp::NumericVector green(256);
Expand Down Expand Up @@ -300,7 +300,7 @@ inline SEXP colour_value_hex(

colourvalues::palette_utils::resolve_palette( palette, red, green, blue, alpha );

Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type, x_size );
Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type );

Rcpp::IntegerVector summary_values = Rcpp::sort_unique( x );
Rcpp::NumericVector summary_numbers = Rcpp::as< Rcpp::NumericVector >( summary_values );
Expand Down Expand Up @@ -335,7 +335,7 @@ inline SEXP colour_value_hex(
int x_size = x.size();
int alpha_type = colourvalues::alpha::make_alpha_type( alpha.size(), x_size, 0 );

Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type, x_size );
Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type );

Rcpp::NumericVector red(256);
Rcpp::NumericVector green(256);
Expand Down
10 changes: 5 additions & 5 deletions inst/include/colourvalues/colours/colours_rgb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace colours_rgb {

colourvalues::palette_utils::resolve_palette( palette, red, green, blue, alpha );

Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type, x_size );
Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type );

if ( n_summaries > 0 ) {
Rcpp::NumericVector summary = colourvalues::summary::numeric_summary( x, n_summaries );
Expand Down Expand Up @@ -129,7 +129,7 @@ namespace colours_rgb {
int x_size = x.size();
int alpha_type = colourvalues::alpha::make_alpha_type( alpha.size(), x_size, 0 );

Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type, x_size );
Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type );

Rcpp::NumericVector red(256);
Rcpp::NumericVector green(256);
Expand Down Expand Up @@ -220,7 +220,7 @@ namespace colours_rgb {
int x_size = x.size();
int alpha_type = colourvalues::alpha::make_alpha_type( alpha.size(), x_size, 0 );

Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type, x_size );
Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type );

Rcpp::NumericVector red(256);
Rcpp::NumericVector green(256);
Expand Down Expand Up @@ -279,7 +279,7 @@ namespace colours_rgb {

colourvalues::palette_utils::resolve_palette( palette, red, green, blue, alpha );

Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type, x_size );
Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type );

Rcpp::IntegerVector summary_values = Rcpp::sort_unique( x );
Rcpp::NumericVector summary_numbers = Rcpp::as< Rcpp::NumericVector >( summary_values );
Expand Down Expand Up @@ -314,7 +314,7 @@ namespace colours_rgb {
int x_size = x.size();
int alpha_type = colourvalues::alpha::make_alpha_type( alpha.size(), x_size, 0 );

Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type, x_size );
Rcpp::NumericVector alpha_full = colourvalues::alpha::validate_alpha( alpha, alpha_type );

Rcpp::NumericVector red(256);
Rcpp::NumericVector green(256);
Expand Down
13 changes: 13 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// rcpp_validate_alpha
Rcpp::NumericVector rcpp_validate_alpha(Rcpp::NumericVector alpha, int alpha_type);
RcppExport SEXP _colourvalues_rcpp_validate_alpha(SEXP alphaSEXP, SEXP alpha_typeSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::NumericVector >::type alpha(alphaSEXP);
Rcpp::traits::input_parameter< int >::type alpha_type(alpha_typeSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_validate_alpha(alpha, alpha_type));
return rcpp_result_gen;
END_RCPP
}
// rcpp_get_format_type
std::string rcpp_get_format_type(SEXP palette_type);
RcppExport SEXP _colourvalues_rcpp_get_format_type(SEXP palette_typeSEXP) {
Expand Down Expand Up @@ -697,6 +709,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_colourvalues_rcpp_convert_hex_to_rgb", (DL_FUNC) &_colourvalues_rcpp_convert_hex_to_rgb, 1},
{"_colourvalues_rcpp_convert_rgb_mat_to_hex", (DL_FUNC) &_colourvalues_rcpp_convert_rgb_mat_to_hex, 1},
{"_colourvalues_rcpp_convert_rgb_vec_to_hex", (DL_FUNC) &_colourvalues_rcpp_convert_rgb_vec_to_hex, 1},
{"_colourvalues_rcpp_validate_alpha", (DL_FUNC) &_colourvalues_rcpp_validate_alpha, 2},
{"_colourvalues_rcpp_get_format_type", (DL_FUNC) &_colourvalues_rcpp_get_format_type, 1},
{"_colourvalues_rcpp_get_r_class", (DL_FUNC) &_colourvalues_rcpp_get_r_class, 1},
{"_colourvalues_rcpp_vector_type", (DL_FUNC) &_colourvalues_rcpp_vector_type, 2},
Expand Down
10 changes: 10 additions & 0 deletions src/alpha.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#include "colourvalues/alpha/alpha.hpp"

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::NumericVector rcpp_validate_alpha( Rcpp::NumericVector alpha, int alpha_type ) {
return colourvalues::alpha::validate_alpha( alpha, alpha_type );
}

40 changes: 40 additions & 0 deletions tests/testthat/test-alpha.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,43 @@ test_that("alpha not updated by reference", {
expect_equal( df$o, rep(5,5) )

})


test_that("alpha is validated",{

#define ALPHA_UNKNOWN 0
#define ALPHA_PALETTE 1 // the alpha is on the palette
#define ALPHA_VECTOR 2 // the alpha is a variable / vector
#define ALPHA_CONSTANT 3 // the alpha is a constant

x <- 1:5
expect_equal( colourvalues:::rcpp_validate_alpha(x, 1), 1:5 )

a <- colourvalues:::rcpp_validate_alpha(x, 2)
expect_true( a[1] == 0.0 )
expect_true( a[5] == 255.0 )

expect_equal( colourvalues:::rcpp_validate_alpha(x, 3), rep(1,5) )

x <- 1:3
expect_equal( colourvalues:::rcpp_validate_alpha(x, 1), 1:3 )

a <- colourvalues:::rcpp_validate_alpha(x, 2) ## < 5 vals get filled with mean
b <- colourvalues:::rcpp_validate_alpha(c(1,2,3,2,2), 2)
expect_equal(a, b)

expect_equal( colourvalues:::rcpp_validate_alpha(x, 3), rep(1,5) )

x <- 1:10
expect_equal( colourvalues:::rcpp_validate_alpha(x, 1), 1:10 )

a <- colourvalues:::rcpp_validate_alpha(x, 2)
expect_true( a[1] == 0.0 )
expect_true( a[10] == 255.0 )

expect_equal( colourvalues:::rcpp_validate_alpha(x, 3), rep(1,5) )

x <- 3:4
expect_equal( colourvalues:::rcpp_validate_alpha(x, 3), rep(x[1], 5) )

})

0 comments on commit d9aa664

Please sign in to comment.