File: lstopo-color.c

package info (click to toggle)
hwloc 1.4.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,692 kB
  • sloc: ansic: 23,568; sh: 12,105; xml: 1,478; makefile: 1,251; csh: 138; php: 8
file content (58 lines) | stat: -rw-r--r-- 978 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * Copyright © 2009 CNRS
 * Copyright © 2009 inria.  All rights reserved.
 * Copyright © 2009 Université Bordeaux 1
 * See COPYING in top-level directory.
 */

#include <stdlib.h>
#include <stdio.h>

#include "lstopo.h"

static struct color {
  int r, g, b;
} *colors;

static int numcolors;

static int
find_color(int r, int g, int b)
{
  int i;

  for (i = 0; i < numcolors; i++)
    if (colors[i].r == r && colors[i].g == g && colors[i].b == b)
      return i;

  return -1;
}

int
rgb_to_color(int r, int g, int b)
{
  int color = find_color(r, g, b);

  if (color != -1)
    return color;

  fprintf(stderr, "color #%02x%02x%02x not declared\n", r, g, b);
  exit(EXIT_FAILURE);
}

int
declare_color(int r, int g, int b)
{
  int color = find_color(r, g, b);

  if (color != -1)
    return color;

  color = numcolors++;
  colors = realloc(colors, sizeof(*colors) * (numcolors));
  colors[color].r = r;
  colors[color].g = g;
  colors[color].b = b;

  return color;
}