C API

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(colorfast)

{colorfast} exports three C functions for use in other packages.

To use these functions:

void col_to_rgb(const char *col, uint8_t ptr[4]);
uint32_t col_to_int(const char *col); 
void int_to_col(uint32_t icol, char buf[10]);

String color to vector of RGBA values

void col_to_rgb(const char *col, uint8_t ptr[4])

The R call col_to_rgb('red'), can be called via the C api as:

#include <stdint.h>
#include <colorfast.h>

void convert_col_to_rgb(const char *col) {
  uint8_t values[4];
  col_to_rgb(col, values);

  for (int i = 0; i < 4; ++i) {
    printf("%i ", values[i]);
  }
}
library(callme)
code = r"(
#include <stdint.h>
#include <colorfast.h>

void convert_col_to_rgb(const char *col) {
  uint8_t values[4];
  col_to_rgb(col, values);

  for (int i = 0; i < 4; ++i) {
    printf("%i ", values[i]);
  }
}


SEXP test() {
  convert_col_to_rgb("red");
  return R_NilValue;
}
)"

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Find the location of nara.h and include its directory in the search path
# using C Pre-Processor flags (PKG_CPPFLAGS)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nara_h <- system.file("include", "colorfast.h", package = "colorfast", mustWork = TRUE)
cpp_include = paste0("-I", dirname(nara_h))
callme::compile(code, PKG_CPPFLAGS = cpp_include)


test()

String color to packed integer color

void col_to_int(const char *col)

#include <stdint.h>
#include <colorfast.h>

void convert_col_to_int(const char *col) {
  uint32_t value = col_to_int(col);

  printf("%i ", value);
}
library(callme)
code = r"(
#include <stdint.h>
#include <colorfast.h>

void convert_col_to_int(const char *col) {
  uint32_t value = col_to_int(col);

  printf("%i ", value);
}


SEXP test() {
  convert_col_to_int("red");
  return R_NilValue;
}
)"

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Find the location of nara.h and include its directory in the search path
# using C Pre-Processor flags (PKG_CPPFLAGS)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nara_h <- system.file("include", "colorfast.h", package = "colorfast", mustWork = TRUE)
cpp_include = paste0("-I", dirname(nara_h))
callme::compile(code, PKG_CPPFLAGS = cpp_include)


test()

Packed integer color to hexadecimal color

void int_to_col(uint32_t icol, char buf[10])

#include <stdint.h>
#include <colorfast.h>

void convert_int_to_col(uint32_t icol) {
  char buf[10];
  int_to_col(icol, buf);

  printf("%s\n", buf);
}
library(callme)
code = r"(
#include <stdint.h>
#include <colorfast.h>

void convert_int_to_col(uint32_t icol) {
  char buf[10];
  int_to_col(icol, buf);

  printf("%s\n", buf);
}


SEXP test() {
  convert_int_to_col(123456);
  return R_NilValue;
}
)"

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Find the location of nara.h and include its directory in the search path
# using C Pre-Processor flags (PKG_CPPFLAGS)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nara_h <- system.file("include", "colorfast.h", package = "colorfast", mustWork = TRUE)
cpp_include = paste0("-I", dirname(nara_h))
callme::compile(code, PKG_CPPFLAGS = cpp_include)


test()


Try the colorfast package in your browser

Any scripts or data that you put into this service are public.

colorfast documentation built on April 4, 2025, 1:03 a.m.