mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-07 12:49:46 +00:00
commit
d2fb107810
4 changed files with 71 additions and 15 deletions
|
@ -824,13 +824,23 @@ module HSL(h,s=1,l=0.5,a=1) color(HSL(h,s,l),a) children();
|
||||||
// rgb = HSV(h=270,s=0.75,v=0.9);
|
// rgb = HSV(h=270,s=0.75,v=0.9);
|
||||||
// color(rgb) cube(60, center=true);
|
// color(rgb) cube(60, center=true);
|
||||||
function HSV(h,s=1,v=1) =
|
function HSV(h,s=1,v=1) =
|
||||||
|
assert(s>=0 && s<=1)
|
||||||
|
assert(v>=0 && v<=1)
|
||||||
let(
|
let(
|
||||||
h = posmod(h,360),
|
h = posmod(h,360),
|
||||||
v2=v*(1-s),
|
c = v * s,
|
||||||
r=lookup(h,[[0,v], [60,v], [120,v2], [240,v2], [300,v], [360,v]]),
|
hprime = h/60,
|
||||||
g=lookup(h,[[0,v2], [60,v], [180,v], [240,v2], [360,v2]]),
|
x = c * (1- abs(hprime % 2 - 1)),
|
||||||
b=lookup(h,[[0,v2], [120,v2], [180,v], [300,v], [360,v2]])
|
rgbprime = hprime <=1 ? [c,x,0]
|
||||||
) [r,g,b];
|
: hprime <=2 ? [x,c,0]
|
||||||
|
: hprime <=3 ? [0,c,x]
|
||||||
|
: hprime <=4 ? [0,x,c]
|
||||||
|
: hprime <=5 ? [x,0,c]
|
||||||
|
: hprime <=6 ? [c,0,x]
|
||||||
|
: [0,0,0],
|
||||||
|
m=v-c
|
||||||
|
)
|
||||||
|
rgbprime+[m,m,m];
|
||||||
|
|
||||||
module HSV(h,s=1,v=1,a=1) color(HSV(h,s,v),a) children();
|
module HSV(h,s=1,v=1,a=1) color(HSV(h,s,v),a) children();
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,23 @@ module test_v_ceil() {
|
||||||
test_v_ceil();
|
test_v_ceil();
|
||||||
|
|
||||||
|
|
||||||
|
module test_v_lookup() {
|
||||||
|
lup = [[4, [3,4,5]], [5, [5,6,7]], [6, [4,5,6]]];
|
||||||
|
assert_equal(v_lookup(3,lup), [3,4,5]);
|
||||||
|
assert_equal(v_lookup(3.5,lup), [3,4,5]);
|
||||||
|
assert_equal(v_lookup(4,lup), [3,4,5]);
|
||||||
|
assert_approx(v_lookup(4.2,lup), [3.4,4.4,5.4]);
|
||||||
|
assert_equal(v_lookup(4.5,lup), [4,5,6]);
|
||||||
|
assert_equal(v_lookup(5,lup), [5,6,7]);
|
||||||
|
assert_approx(v_lookup(5.2,lup), [4.8,5.8,6.8]);
|
||||||
|
assert_equal(v_lookup(5.5,lup), [4.5,5.5,6.5]);
|
||||||
|
assert_equal(v_lookup(6,lup), [4,5,6]);
|
||||||
|
assert_equal(v_lookup(6.5,lup), [4,5,6]);
|
||||||
|
assert_equal(v_lookup(7,lup), [4,5,6]);
|
||||||
|
}
|
||||||
|
test_v_lookup();
|
||||||
|
|
||||||
|
|
||||||
module test_v_mul() {
|
module test_v_mul() {
|
||||||
assert_equal(v_mul([3,4,5], [8,7,6]), [24,28,30]);
|
assert_equal(v_mul([3,4,5], [8,7,6]), [24,28,30]);
|
||||||
assert_equal(v_mul([1,2,3], [4,5,6]), [4,10,18]);
|
assert_equal(v_mul([1,2,3], [4,5,6]), [4,10,18]);
|
||||||
|
|
|
@ -253,9 +253,11 @@ easier to select colors using other color schemes. You can use the HSL or Hue-S
|
||||||
color scheme with the `HSL()` module:
|
color scheme with the `HSL()` module:
|
||||||
|
|
||||||
```openscad
|
```openscad
|
||||||
for (h=[0:0.1:1], s=[0:0.1:1], l=[0:0.1:1]) {
|
n = 10; size = 100/n;
|
||||||
translate(100*[h,s,l]) {
|
for (a=count(n), b=count(n), c=count(n)) {
|
||||||
HSL(h*360,1-s,l) cube(10,center=true);
|
let( h=360*a/n, s=1-b/(n-1), l=c/(n-1))
|
||||||
|
translate(size*[a,b,c]) {
|
||||||
|
HSL(h,s,l) cube(size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -263,9 +265,11 @@ for (h=[0:0.1:1], s=[0:0.1:1], l=[0:0.1:1]) {
|
||||||
You can use the HSV or Hue-Saturation-Value color scheme with the `HSV()` module:
|
You can use the HSV or Hue-Saturation-Value color scheme with the `HSV()` module:
|
||||||
|
|
||||||
```openscad
|
```openscad
|
||||||
for (h=[0:0.1:1], s=[0:0.1:1], v=[0:0.1:1]) {
|
n = 10; size = 100/n;
|
||||||
translate(100*[h,s,v]) {
|
for (a=count(n), b=count(n), c=count(n)) {
|
||||||
HSV(h*360,1-s,v) cube(10,center=true);
|
let( h=360*a/n, s=1-b/(n-1), v=c/(n-1))
|
||||||
|
translate(size*[a,b,c]) {
|
||||||
|
HSV(h,s,v) cube(size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
25
vectors.scad
25
vectors.scad
|
@ -107,6 +107,31 @@ function v_ceil(v) =
|
||||||
[for (x=v) ceil(x)];
|
[for (x=v) ceil(x)];
|
||||||
|
|
||||||
|
|
||||||
|
// Function: v_lookup()
|
||||||
|
// Description:
|
||||||
|
// Works just like the built-in function [`lookup()`](https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Mathematical_Functions#lookup), except that it can also interpolate between vector result values of the same length.
|
||||||
|
// Arguments:
|
||||||
|
// x = The scalar value to look up.
|
||||||
|
// v = A list of [KEY,VAL] pairs. KEYs are scalars. VALs should either all be scalar, or all be vectors of the same length.
|
||||||
|
// Example:
|
||||||
|
// x = v_lookup(4.5, [[4, [3,4,5]], [5, [5,6,7]]]); // Returns: [4,5,6]
|
||||||
|
function v_lookup(x, v) =
|
||||||
|
is_num(v[0][1])? lookup(x,v) :
|
||||||
|
let(
|
||||||
|
i = lookup(x, [for (i=idx(v)) [v[i].x,i]]),
|
||||||
|
vlo = v[floor(i)],
|
||||||
|
vhi = v[ceil(i)],
|
||||||
|
lo = vlo[1],
|
||||||
|
hi = vhi[1]
|
||||||
|
)
|
||||||
|
assert(is_vector(lo) && is_vector(hi),
|
||||||
|
"Result values must all be numbers, or all be vectors.")
|
||||||
|
assert(len(lo) == len(hi), "Vector result values must be the same length")
|
||||||
|
vlo.x == vhi.x? vlo[1] :
|
||||||
|
let( u = (x - vlo.x) / (vhi.x - vlo.x) )
|
||||||
|
lerp(lo,hi,u);
|
||||||
|
|
||||||
|
|
||||||
// Function: unit()
|
// Function: unit()
|
||||||
// Usage:
|
// Usage:
|
||||||
// unit(v, [error]);
|
// unit(v, [error]);
|
||||||
|
|
Loading…
Reference in a new issue