star-cmap

Mapping values onto color ramps
git clone git://git.meso-star.fr/star-cmap.git
Log | Files | Refs | README | LICENSE

test_scmap_fetch_color.c (3509B)


      1 /* Copyright (C) 2020, 2021, 2023 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #define _POSIX_C_SOURCE 200112L /* nextafter support */
     17 
     18 #include "scmap.h"
     19 
     20 #include <rsys/double3.h>
     21 #include <rsys/mem_allocator.h>
     22 
     23 #include <math.h>
     24 
     25 static const double colors[] = {
     26   0.1, 0.1, 0.1,
     27   0.2, 0.2, 0.2,
     28   0.3, 0.3, 0.3,
     29   0.4, 0.4, 0.4,
     30   0.5, 0.5, 0.5
     31 };
     32 static const size_t ncolors = sizeof(colors)/(sizeof(double)*3);
     33 
     34 static void
     35 get_color(const size_t i, double col[3], void* context)
     36 {
     37   (void)context;
     38   CHK(col);
     39   CHK(i < ncolors);
     40   col[0] = colors[i*3+0];
     41   col[1] = colors[i*3+1];
     42   col[2] = colors[i*3+2];
     43 }
     44 
     45 int
     46 main(int argc, char** argv)
     47 {
     48   struct scmap* scmap = NULL;
     49   struct scmap_palette palette = SCMAP_PALETTE_NULL;
     50   double col[3];
     51   (void)argc, (void)argv;
     52 
     53   palette.get_color = get_color;
     54   palette.ncolors = ncolors;
     55   CHK(scmap_create(NULL, NULL, 1, &palette, &scmap) == RES_OK);
     56 
     57   CHK(scmap_fetch_color(NULL, 0, SCMAP_FILTER_NEAREST, col) == RES_BAD_ARG);
     58   CHK(scmap_fetch_color(scmap, -1, SCMAP_FILTER_NEAREST, col) == RES_BAD_ARG);
     59   CHK(scmap_fetch_color(scmap, 0, -1, col) == RES_BAD_ARG);
     60   CHK(scmap_fetch_color(scmap, 0, SCMAP_FILTER_NEAREST, NULL) == RES_BAD_ARG);
     61 
     62   CHK(scmap_fetch_color(scmap, 0, SCMAP_FILTER_NEAREST, col) == RES_OK);
     63   CHK(col[0] == col[1] && col[1] == col[2] && col[2] == 0.1);
     64 
     65   CHK(scmap_fetch_color(scmap, 0.124, SCMAP_FILTER_NEAREST, col) == RES_OK);
     66   CHK(col[0] == col[1] && col[1] == col[2] && col[2] == 0.1);
     67   CHK(scmap_fetch_color(scmap, 0.126, SCMAP_FILTER_NEAREST, col) == RES_OK);
     68   CHK(col[0] == col[1] && col[1] == col[2] && col[2] == 0.2);
     69   CHK(scmap_fetch_color(scmap, 0.25, SCMAP_FILTER_LINEAR, col) == RES_OK);
     70   CHK(col[0] == col[1] && col[1] == col[2] && col[2] == 0.2);
     71   CHK(scmap_fetch_color(scmap, 0.5, SCMAP_FILTER_LINEAR, col) == RES_OK);
     72   CHK(col[0] == col[1] && col[1] == col[2] && col[2] == 0.3);
     73   CHK(scmap_fetch_color(scmap, 0.75, SCMAP_FILTER_LINEAR, col) == RES_OK);
     74   CHK(col[0] == col[1] && col[1] == col[2] && col[2] == 0.4);
     75   CHK(scmap_fetch_color(scmap, 1, SCMAP_FILTER_LINEAR, col) == RES_OK);
     76   CHK(col[0] == col[1] && col[1] == col[2] && col[2] == 0.5);
     77 
     78   CHK(scmap_fetch_color(scmap, nextafter(1, 2), SCMAP_FILTER_LINEAR, col)
     79     == RES_BAD_ARG);
     80 
     81   CHK(scmap_fetch_color(scmap, 0.125, SCMAP_FILTER_LINEAR, col) == RES_OK);
     82   CHK(col[0] == col[1] && col[1] == col[2] && col[2] == (0.1+0.2)*0.5);
     83   CHK(scmap_fetch_color(scmap, 0.7, SCMAP_FILTER_LINEAR, col) == RES_OK);
     84   CHK(col[0] == col[1] && col[1] == col[2] && col[2]);
     85   CHK(eq_eps(col[0], 0.8 * (0.4 - 0.3) + 0.3, 1.e-8));
     86   CHK(scmap_fetch_color(scmap, 0.85, SCMAP_FILTER_LINEAR, col) == RES_OK);
     87   CHK(col[0] == col[1] && col[1] == col[2] && col[2]);
     88   CHK(eq_eps(col[0], 0.4 * (0.5 - 0.4) + 0.4, 1.e-8));
     89 
     90   CHK(scmap_ref_put(scmap) == RES_OK);
     91 
     92   CHK(mem_allocated_size() == 0);
     93   return 0;
     94 }
     95