blob: c632ccdc1243b1b38aa75b792b83b2d6da925924 (
plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#include "emb-thread.h"
#include "emb-logging.h"
#include <stdio.h>
#include <stdlib.h>
int embThread_findNearestColor(EmbColor color, EmbThreadList* colors)
{
double currentClosestValue = 9999999;
int closestIndex = -1;
int red = color.r;
int green = color.g;
int blue = color.b;
int i = 0;
EmbThreadList* currentThreadItem = colors;
while(currentThreadItem != NULL)
{
int deltaRed;
int deltaBlue;
int deltaGreen;
double dist;
EmbColor c;
c = currentThreadItem->thread.color;
deltaRed = red - c.r;
deltaBlue = green - c.g;
deltaGreen = blue - c.b;
dist = sqrt((double)(deltaRed * deltaRed) + (deltaBlue * deltaBlue) + (deltaGreen * deltaGreen));
if(dist <= currentClosestValue)
{
currentClosestValue = dist;
closestIndex = i;
}
currentThreadItem = currentThreadItem->next;
i++;
}
return closestIndex;
}
int embThread_findNearestColorInArray(EmbColor color, EmbThread* colorArray, int count)
{
double currentClosestValue = 9999999;
int closestIndex = -1;
int red = color.r;
int green = color.g;
int blue = color.b;
int i = 0;
for(i = 0; i < count; i++)
{
int deltaRed;
int deltaBlue;
int deltaGreen;
double dist;
EmbColor c;
c = colorArray[i].color;
deltaRed = red - c.r;
deltaBlue = green - c.g;
deltaGreen = blue - c.b;
dist = sqrt((double)(deltaRed * deltaRed) + (deltaBlue * deltaBlue) + (deltaGreen * deltaGreen));
if(dist <= currentClosestValue)
{
currentClosestValue = dist;
closestIndex = i;
}
}
return closestIndex;
}
EmbThread embThread_getRandom(void)
{
EmbThread c;
c.color.r = rand()%256;
c.color.g = rand()%256;
c.color.b = rand()%256;
c.description = "random";
c.catalogNumber = "";
return c;
}
EmbThreadList* embThreadList_create(EmbThread data)
{
EmbThreadList* heapThreadList = (EmbThreadList*)malloc(sizeof(EmbThreadList));
if(!heapThreadList) { embLog_error("emb-thread.c embThreadList_create(), cannot allocate memory for heapThreadList\n"); return 0; }
heapThreadList->thread = data;
heapThreadList->next = 0;
return heapThreadList;
}
EmbThreadList* embThreadList_add(EmbThreadList* pointer, EmbThread data)
{
if(!pointer) { embLog_error("emb-thread.c embThreadList_add(), pointer argument is null\n"); return 0; }
if(pointer->next) { embLog_error("emb-thread.c embThreadList_add(), pointer->next should be null\n"); return 0; }
pointer->next = (EmbThreadList*)malloc(sizeof(EmbThreadList));
if(!pointer->next) { embLog_error("emb-thread.c embThreadList_add(), cannot allocate memory for pointer->next\n"); return 0; }
pointer = pointer->next;
pointer->thread = data;
pointer->next = 0;
return pointer;
}
EmbThread embThreadList_getAt(EmbThreadList* pointer, int num)
{
/* TODO: pointer safety */
int i = 0;
for(i = 0; i < num; i++)
{
if(pointer->next)
{
pointer = pointer->next;
}
}
return pointer->thread;
}
int embThreadList_count(EmbThreadList* pointer)
{
int i = 1;
if(!pointer) return 0;
while(pointer->next)
{
pointer = pointer->next;
i++;
}
return i;
}
int embThreadList_empty(EmbThreadList* pointer)
{
if(!pointer)
return 1;
return 0;
}
void embThreadList_free(EmbThreadList* pointer)
{
EmbThreadList* tempPointer = pointer;
EmbThreadList* nextPointer = 0;
while(tempPointer)
{
nextPointer = tempPointer->next;
free(tempPointer);
tempPointer = nextPointer;
}
pointer = 0;
}
/* kate: bom off; indent-mode cstyle; indent-width 4; replace-trailing-space-save on; */
|