Browse Source

create object oriented python library pybind11

master
alistair 1 year ago
parent
commit
2974d506e9
  1. 9
      Makefile
  2. 344
      colours.c
  3. 243
      colours.cpp
  4. 13
      test.py

9
Makefile

@ -1,7 +1,12 @@ @@ -1,7 +1,12 @@
python: colours.o
c++ -O2 -Wall -shared -std=c++17 -fPIC -I/usr/include/python3.11 -I/home/alistair/.local/lib/python3.11/site-packages/pybind11/include colours.cpp colours.o -o colourpal.cpython-311-x86_64-linux-gnu.so
debug: colours.c colours.h
colours.o: colours.c colours.h
gcc -O2 -lm -fPIC -c colours.c
test: colours.c colours.h
gcc -lm -c colours.c
debug: colours.c colours.h
gcc -lm -c colours.c -g
gcc -lm -fPIC -c colours.c -g

344
colours.c

@ -1,238 +1,238 @@ @@ -1,238 +1,238 @@
#include "colours.h"
#include <assert.h>
struct colour get_random_color(unsigned int seed) {
srand(seed);
int red = rand() % 255;
int blue = rand() % 255;
int green = rand() % 255;
struct colour col;
col.r = red;
col.g = green;
col.b = blue;
return col;
srand(seed);
int red = rand() % 255;
int blue = rand() % 255;
int green = rand() % 255;
struct colour col;
col.r = red;
col.g = green;
col.b = blue;
return col;
}
double m_min(double arr[], int len) {
double largest = arr[0];
for (int i = 0; i < len; i ++) {
if (arr[i] < largest) {
largest = arr[i];
}
double largest = arr[0];
for (int i = 0; i < len; i++) {
if (arr[i] < largest) {
largest = arr[i];
}
return largest;
}
return largest;
}
double m_max(double arr[], int len) {
double largest = arr[0];
for (int i = 0; i < len; i ++) {
if (arr[i] > largest) {
largest = arr[i];
}
double largest = arr[0];
for (int i = 0; i < len; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
return largest;
}
return largest;
}
bool m_equal(double a, double b) {
return (a - b) * (a - b) < 0.00001;
}
bool m_equal(double a, double b) { return (a - b) * (a - b) < 0.00001; }
// https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB
// h = [0,360], s = [0,1], v = [0,1]
// h = [0,360], s = [0,1], v = [0,1]
// if s == 0, then h = -1 (undefined)
struct colour get_hs_l_v(struct colour c, enum colour_space sp) {
struct colour ret;
memset(&ret, 0, sizeof(struct colour));
double r = (double)c.r / 255;
double g = (double)c.g / 255;
double b = (double)c.b / 255;
double arr[] = {r, g, b};
double max = m_max(arr, 3);
double min = m_min(arr, 3);
ret.v = max;
double chroma = max - min;
double lum = (max + min) / 2;
ret.l = lum;
if (m_equal(chroma, 0)) {
ret.h = 0;
} else if (m_equal(ret.v,r)) {
ret.h = (g - b) / chroma;
} else if (m_equal(max,g)) {
ret.h = 2 + (b - r) / chroma;
} else if (m_equal(max,b)) {
ret.h = 4 + (r - g) / chroma;
if (c.sp == sp) {
return c;
}
assert(c.sp == CS_RGB);
struct colour ret;
memset(&ret, 0, sizeof(struct colour));
ret.sp = sp;
double r = (double)c.r / 255;
double g = (double)c.g / 255;
double b = (double)c.b / 255;
double arr[] = {r, g, b};
double max = m_max(arr, 3);
double min = m_min(arr, 3);
ret.v = max;
double chroma = max - min;
double lum = (max + min) / 2;
ret.l = lum;
if (m_equal(chroma, 0)) {
ret.h = 0;
} else if (m_equal(ret.v, r)) {
ret.h = (g - b) / chroma;
} else if (m_equal(max, g)) {
ret.h = 2 + (b - r) / chroma;
} else if (m_equal(max, b)) {
ret.h = 4 + (r - g) / chroma;
} else {
printf("NOPE BAD ERROR\n");
}
ret.h *= 60;
ret.h = fmod(ret.h, 360);
if (ret.h < 0) {
ret.h += 360;
}
if (sp == CS_HSV) {
if (m_equal(max, 0)) {
ret.s = 0;
} else {
printf("NOPE BAD ERROR\n");
}
ret.h *= 60;
ret.h = fmod(ret.h, 360);
if (ret.h < 0) {
ret.h += 360;
ret.s = chroma / max;
}
if (sp == CS_HSV) {
if (m_equal(max,0)) {
ret.s = 0;
} else {
ret.s = chroma / max;
}
} else {
double arr[] = {ret.l, 1 - ret.l};
if (m_equal(0, lum) || m_equal(1, lum)) {
ret.s = 0;
} else {
double arr[] = {ret.l, 1 - ret.l};
if (m_equal(0, lum) || m_equal(1, lum)) {
ret.s = 0;
} else {
ret.s = (ret.v - ret.l) / m_min(arr, 2);
}
ret.s = (ret.v - ret.l) / m_min(arr, 2);
}
ret.sp = CS_HSL;
return ret;
}
ret.sp = sp;
return ret;
}
struct colour get_hsl(struct colour c) {
return get_hs_l_v(c, CS_HSL);
}
struct colour get_hsl(struct colour c) { return get_hs_l_v(c, CS_HSL); }
struct colour get_hsv(struct colour c) {
return get_hs_l_v(c, CS_HSV);
}
struct colour get_hsv(struct colour c) { return get_hs_l_v(c, CS_HSV); }
double magic_hsv_function(int n, struct colour c) {
double res = 0;
double k = fmod(n + (c.h / 60), 6);
double arr[] = {k, 4 - k, 1};
double k_b = m_min(arr, 3);
double k_c = 0 > k_b ? 0 : k_b;
res = c.v - c.v * c.s * k_c;
return res;
double res = 0;
double k = fmod(n + (c.h / 60), 6);
double arr[] = {k, 4 - k, 1};
double k_b = m_min(arr, 3);
double k_c = 0 > k_b ? 0 : k_b;
res = c.v - c.v * c.s * k_c;
return res;
}
struct colour get_rgb_from_hsv(struct colour c) {
double r = magic_hsv_function(5, c);
double g = magic_hsv_function(3, c);
double b = magic_hsv_function(1, c);
struct colour res;
res.r = round(r * 255);
res.g = round(g * 255);
res.b = round(b * 255);
return res;
double r = magic_hsv_function(5, c);
double g = magic_hsv_function(3, c);
double b = magic_hsv_function(1, c);
struct colour res;
res.sp = CS_RGB;
res.r = round(r * 255);
res.g = round(g * 255);
res.b = round(b * 255);
return res;
}
double magic_hsl_function(int n, struct colour c) {
double arr[] = {c.l, 1-c.l};
double a = c.s * ( c.l < (1-c.l) ? c.l : 1 - c.l );
double k = fmod(n + c.h / 30, 12);
double arr[] = {c.l, 1 - c.l};
double a = c.s * (c.l < (1 - c.l) ? c.l : 1 - c.l);
double k = fmod(n + c.h / 30, 12);
double b[] = {k - 3, 9 -k, 1};
double d[] = {-1, m_min(b, 3)};
return c.l - a * (d[0] > d[1] ? d[0] : d[1]);
double b[] = {k - 3, 9 - k, 1};
double d[] = {-1, m_min(b, 3)};
return c.l - a * (d[0] > d[1] ? d[0] : d[1]);
}
struct colour get_rgb_from_hsl(struct colour c) {
double r = magic_hsl_function(0, c);
double g = magic_hsl_function(8, c);
double b = magic_hsl_function(4, c);
struct colour res;
res.r = round(r * 255);
res.g = round(g * 255);
res.b = round(b * 255);
return res;
double r = magic_hsl_function(0, c);
double g = magic_hsl_function(8, c);
double b = magic_hsl_function(4, c);
struct colour res;
res.sp = CS_RGB;
res.r = round(r * 255);
res.g = round(g * 255);
res.b = round(b * 255);
return res;
}
struct colour get_rgb(struct colour c) {
if (c.sp == CS_HSL) {
return get_rgb_from_hsl(c);
}
if (c.sp == CS_HSV) {
return get_rgb_from_hsv(c);
}
if (c.sp == CS_HSL) {
return get_rgb_from_hsl(c);
}
if (c.sp == CS_HSV) {
return get_rgb_from_hsv(c);
}
struct colour d;
memset(&d, 0, sizeof(struct colour));
return d;
return c;
}
int compare_perceived_lum(const void* c1, const void* c2) {
struct colour rgb_c1 = *(struct colour*)c1;
struct colour rgb_c2 = *(struct colour*)c2;
int compare_perceived_lum(const void *c1, const void *c2) {
struct colour rgb_c1 = *(struct colour *)c1;
struct colour rgb_c2 = *(struct colour *)c2;
double lum1 = 0.299*rgb_c1.r + 0.587*rgb_c1.g + 0.114*rgb_c1.b;
double lum2 = 0.299*rgb_c2.r + 0.587*rgb_c2.g + 0.114*rgb_c2.b;
return lum1 - lum2;
double lum1 = 0.299 * rgb_c1.r + 0.587 * rgb_c1.g + 0.114 * rgb_c1.b;
double lum2 = 0.299 * rgb_c2.r + 0.587 * rgb_c2.g + 0.114 * rgb_c2.b;
return lum1 - lum2;
}
struct colour *get_adjacent(struct colour base, int deg, int num) {
struct colour col = get_hsl(base);
num += 1;
struct colour col = get_hsl(base);
num += 1;
struct colour* colours = calloc(sizeof(struct colour), num);
struct colour *colours = calloc(sizeof(struct colour), num);
for (int i = 0; i < num; i++) {
int m = (i % 2 == 0) ? -i : i;
for (int i = 0; i < num; i++) {
int m = (i % 2 == 0) ? -i : i;
colours[i] = col;
colours[i].h += m * deg;
colours[i].h += fmod(colours[i].h, 360);
}
colours[i] = col;
colours[i].h += m * deg;
colours[i].h += fmod(colours[i].h, 360);
}
struct colour *ret_colours = calloc(num, sizeof(struct colour) * num);
struct colour *ret_colours = calloc(num, sizeof(struct colour) * num);
for (int i = 0; i < num; i++) {
ret_colours[i] = get_rgb(colours[i]);
}
for (int i = 0; i < num; i++) {
ret_colours[i] = get_rgb(colours[i]);
}
// sort from dark to bright
qsort(ret_colours, num, sizeof(struct colour), compare_perceived_lum);
// sort from dark to bright
qsort(ret_colours, num, sizeof(struct colour), compare_perceived_lum);
free(colours);
return ret_colours;
free(colours);
return ret_colours;
}
void print_colour(struct colour c) {
char *colour = calloc(20, sizeof(char));
sprintf(colour, "#%02x%02x%02x", c.r, c.g, c.b);
printf("\x1b[38;2;%d;%d;%dm%s\x1b[0m\n", c.r, c.g, c.b, colour);
free(colour);
char *colour = calloc(20, sizeof(char));
sprintf(colour, "#%02x%02x%02x", c.r, c.g, c.b);
printf("\x1b[38;2;%d;%d;%dm%s\x1b[0m\n", c.r, c.g, c.b, colour);
free(colour);
}
void test(int seed) {
struct colour c = get_random_color(seed);
//print_colour(c);
struct colour hsl = get_hsl(c);
struct colour hsv = get_hsv(c);
/*printf("RGB: %d %d %d\n",c.r,c.g,c.b);*/
/*printf("HSL: %f %f %f\n",hsl.h, hsl.s, hsl.l);*/
/*printf("HSV: %f %f %f\n",hsv.h, hsv.s, hsv.v);*/
struct colour *adj = get_adjacent(c, 5, 4);
for (int i = 0; i < 5; i++) {
print_colour(adj[i]);
}
struct colour c = get_random_color(seed);
// print_colour(c);
struct colour hsl = get_hsl(c);
struct colour hsv = get_hsv(c);
/*printf("RGB: %d %d %d\n",c.r,c.g,c.b);*/
/*printf("HSL: %f %f %f\n",hsl.h, hsl.s, hsl.l);*/
/*printf("HSV: %f %f %f\n",hsv.h, hsv.s, hsv.v);*/
struct colour *adj = get_adjacent(c, 5, 4);
for (int i = 0; i < 5; i++) {
print_colour(adj[i]);
}
free(adj);
printf("\n");
free(adj);
printf("\n");
}
void test_print_wheel() {
struct colour c;
c.s = 0.999;
c.v = 0.99;
c.l = 0.5;
c.sp = CS_HSV;
for(int i = 0; i < 360; i+=5) {
c.h = (double)i;
struct colour rgb = get_rgb(c);
print_colour(rgb);
}
struct colour c;
c.s = 0.999;
c.v = 0.99;
c.l = 0.5;
c.sp = CS_HSV;
for (int i = 0; i < 360; i += 5) {
c.h = (double)i;
struct colour rgb = get_rgb(c);
print_colour(rgb);
}
}

243
colours.cpp

@ -0,0 +1,243 @@ @@ -0,0 +1,243 @@
#include <cassert>
#include <iomanip>
#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <sstream>
#include <vector>
namespace py = pybind11;
extern "C" {
#include "colours.h"
}
class Colour {
Colour(struct colour col) : col(col) {}
public:
struct colour col {};
Colour(int r, int g, int b) {
col.r = r;
col.g = g;
col.b = b;
col.sp = colour_space::CS_RGB;
}
Colour(double h, double s, double v) {
col.h = h;
col.s = s;
col.v = v;
col.sp = colour_space::CS_HSV;
}
Colour() {
col = {};
col.r = 255;
}
void set_r(int r) { col.r = r; }
void set_g(int g) { col.g = g; }
void set_b(int b) { col.b = b; }
void set_h(double h) {
if (col.sp == CS_RGB)
col.sp = CS_HSV;
col.h = h;
}
void set_s(double s) {
if (col.sp == CS_RGB)
col.sp = CS_HSV;
col.s = s;
}
void set_v(double v) {
col.v = v;
col.sp = CS_HSV;
}
void set_l(double l) {
col.l = l;
col.sp = CS_HSL;
}
Colour rgb() { return Colour{get_rgb(col)}; }
Colour hsv() { return Colour{get_hsv(col)}; }
Colour hsl() { return Colour{get_hsl(col)}; }
int get_r() {
// assert(col.sp == CS_RGB);
col = get_rgb(col);
return rgb().col.r;
}
int get_g() {
// assert(col.sp == CS_RGB);
col = get_rgb(col);
return col.g;
}
int get_b() {
// assert(col.sp == CS_RGB);
col = get_rgb(col);
return col.b;
}
double get_h() {
// assert(col.sp == CS_HSV || col.sp == CS_HSL);
col = get_hsv(col);
return col.h;
}
double get_s() {
// assert(col.sp == CS_HSV || col.sp == CS_HSL);
col = get_hsv(col);
return col.s;
}
double get_v() {
// assert(col.sp == CS_HSV);
col = get_hsv(col);
return col.v;
}
double get_l() {
assert(col.sp == CS_HSL);
return col.l;
}
std::string to_term() {
std::stringstream s{};
auto col = rgb();
s << "\x1b[38;2;" << col.get_r() << ";" << col.get_g() << ";" << col.get_b()
<< "m";
s << to_hex() << "\x1b[0m";
return s.str();
}
std::string to_hex() {
Colour rgbcol = rgb();
char colour[20];
snprintf(colour, 20, "#%02x%02x%02x", rgbcol.get_r(), rgbcol.get_g(),
rgbcol.get_b());
return std::string(colour);
}
std::string str() {
std::stringstream s{};
s << "rgb(" << col.r << "," << col.g << "," << col.b << "):hsv(" << col.h
<< "," << col.s << "," << col.v << "):hsl(" << col.h << "," << col.s
<< "," << col.l << ");";
return s.str();
}
};
Colour rgb(int r, int g, int b) { return Colour(r, g, b); }
Colour hsv(double h, double s, double v) { return Colour(h, s, v); }
Colour hsl(double h, double s, double l) {
auto c = Colour(h, s, 0.0);
c.set_l(l);
return c;
}
std::vector<Colour> lerp_gradient(Colour from, Colour to, int length) {
std::vector<Colour> gradient{};
gradient.reserve(length);
Colour start = from.rgb();
Colour end = to.rgb();
double r = start.get_r();
double g = start.get_g();
double b = start.get_b();
double r_step = (r - end.get_r()) / (length - 1);
double g_step = (g - end.get_g()) / (length - 1);
double b_step = (b - end.get_b()) / (length - 1);
gradient.push_back(start);
for (int i = 1; i < length - 1; i++) {
gradient.push_back(Colour((int)r, (int)g, (int)b));
r -= r_step;
g -= g_step;
b -= b_step;
}
gradient.push_back(end);
return gradient;
}
std::vector<Colour> make_gradient(Colour from, Colour to, int length) {
std::vector<Colour> gradient{};
gradient.reserve(length);
Colour start = from.hsv();
Colour end = to.hsv();
double h = start.get_h();
double s = start.get_s();
double v = start.get_v();
double left = end.get_h() - start.get_h();
double right = -end.get_h() + start.get_h();
double hue_step = (abs(left) < abs(right) ? left : right) / (length - 1);
hue_step = left / (length - 1);
double sat_step = (end.get_s() - start.get_s()) / (length - 1);
double val_step = (end.get_v() - start.get_v()) / (length - 1);
gradient.push_back(start);
for (int i = 1; i < length; i++) {
gradient.emplace_back(h, s, v);
h = fmod(h + hue_step, 360);
if (h < 0) {
h = 360 + h;
}
s += sat_step;
v += val_step;
}
return gradient;
}
PYBIND11_MODULE(colourpal, m) {
m.doc() = "Colour conversion lib"; // optional module docstring
//
m.def("rgb", &rgb)
.def("hsv", &hsv)
.def("hsl", &hsl)
.def("make_gradient", &make_gradient)
.def("lerp_gradient", &lerp_gradient);
py::class_<Colour>(m, "CColour")
.def(py::init<>())
.def("hexstring", &Colour::to_hex)
.def("terminal", &Colour::to_term)
.def("rgb", &Colour::rgb)
.def("hsv", &Colour::hsv)
.def("hsl", &Colour::hsl)
.def("get_h", &Colour::get_h)
.def("get_s", &Colour::get_s)
.def("get_v", &Colour::get_v)
.def("get_l", &Colour::get_l)
.def("get_r", &Colour::get_r)
.def("get_g", &Colour::get_g)
.def("get_b", &Colour::get_b)
.def("set_h", &Colour::set_h)
.def("set_s", &Colour::set_s)
.def("set_v", &Colour::set_v)
.def("set_l", &Colour::set_l)
.def("set_r", &Colour::set_r)
.def("set_g", &Colour::set_g)
.def("set_b", &Colour::set_b)
.def("str", &Colour::str);
}

13
test.py

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
from colourpal import *
print("start", hsv(1, 0.6, 0.1).str())
print("end", hsv(50, 0.6, 0.1).str())
print("start", hsv(1, 0.6, 0.1).rgb().str())
print("end", hsv(50, 0.6, 0.1).rgb().str())
x = lerp_gradient(hsv(1,0.6,0.1), hsv(50, 0.6, 0.1), 10)
for i in x:
print(i.str(), i.hexstring(), i.str())
Loading…
Cancel
Save