In processing, lets say I have an array:
int[] rgb = {floor(random(0,255)), floor(random(0,255)), floor(random(0,255)};
So this will create an array with [0], [1], and [2] being corresponding red, green. and blue values.
Now let's say that I want to pass the fill() function, which needs 3 integers--red, green, and blue. I could just put fill(rgb[0], rgb[1], rgb[2]);, but that does not seem like the most efficient way of doing this. What I want is to be able to pass something like this to fill():
fill(rgb[0,1,2]); which will do the same thing as fill(rgb[0], rgb[1], rgb[2]);, but will be much less writing. Help is appreciated.
I've never used processing, but you can probably overload the fill method to take an array.
void fill(int[] rgb)
{
fill(rgb[0], rgb[1], rgb[2]);
};
Which you could then easily call like...
int[] rgb = {floor(random(0,255)), floor(random(0,255)), floor(random(0,255)};
fill(rgb);
There is a section in this article called "Too Many Parameters", I think it will help more.
Instead of storing your fields in an array, store them in an object.
Processing has a color datatype that you could use. The color() function takes three arguments, and you can then pass the single color value to the fill() function. Like this:
color myColor = color(random(256), random(256), random(256));
fill(myColor);
You could also create your own class and use that instead.
Related
I'm developing a addon library for p5.js and I need to setup several fill/stroke colors in certain functions.
Is there a way to get the current fill/stroke value so I can ensure that when the user calls said functions he doesn't have to worry about the colors that I set?
Something of this sort:
function foo(){
var tempColor = getFill(); //Hypothetical get color function
// ...
fill(color1); //Some color I use
// ...
fill(color2); //Another color I use
// ...
fill(tempColor); //Reset fill color to user set
}
Edit: Although undocumented, I found some references in p5.js to a curFillColor variable but I didn't find a way to use this.
Hey I don't know if its a bit late. I'm also trying to create an addon library for p5. So what I'm doing is I'm calling push just before I'm using the fill and after that call pop. So that I ensure that the fill is restored.
Refer
https://p5js.org/reference/#/p5/push
P5.js is open source, so you can see exactly what they do when you call the fill() function here:
p5.prototype.fill = function() {
this._renderer._setProperty('_fillSet', true);
this._renderer._setProperty('_doFill', true);
this._renderer.fill.apply(this._renderer, arguments);
return this;
};
Which takes you to the renderer-specific fill variable. You can track that down, but basically: there isn't an easy way to get the current fill color.
What you might want to do is to use a buffer in your library. Don't call fill() on the main sketch; call fill() on the buffer. Do all your drawing to the buffer, and then draw that buffer to the sketch.
The Question:
Is there a SASS function/technique that transforms a HEX value to a simple RGB string.
Simple here meaning just a string without it being enclosed in rgb() ?
E.g: #D50000 --> "213,0,0"
Why I need this:
I'm using Material Design Lite as my UI 'framework'. More specifically I'm using the SASS version so I can tweak the color variables according to my app's style-guide.
For some reason the color variables in _variables.scss of MDL take this format for color definitions:
$color-primary: "0,0,0" !default; // supposed to be black
which is really, really odd. I expected, at most, something along the lines of
$color-primary: rgba(0,0,0,1) !default;
My color variables are stored in another file called _globals.scss in which I store my variables in regular HEX format so I can easily reuse them in other places:
$brand-primary: #FA3166;
$brand-primary-dark: #E02C59;
I don't want to define 2 times my colours (1 HEX & 1 MDL-compatible RGB string), hence the reason I need to transform HEX to RGB-string.
#nicholas-kyriakides's answer works perfectly fine, but here is a more concise function using Sass interpolation.
#function hexToRGBString($hexColor) {
#return "#{red($hexColor)},#{green($hexColor)},#{blue($hexColor)}";
}
You can pass in either a hex either explicity or from rgb() or rgba() with opacity as 1.
For example:
$color-white: hexToRGBString(#fff) => "255,255,255"
$color-white: hexToRGBString(rgb(255,255,255)) => "255,255,255"
$color-white: hexToRGBString(rgba(#fff,1)) => "255,255,255"
I've hacked around it with a SASS function:
#function hexToString($hexColor) {
// 0.999999 val in alpha actually compiles to 1.0
$rgbaVal: inspect(rgba($hexColor,0.9999999));
// slice substring between 'rgba(' and '1.0)'
#return str-slice($rgbaVal, 6, str-length($rgbaVal)-6);
}
Usage:
$brand-primary: #333;
$color-primary: hexToString($brand-primary);
I think the MDL team intended to have a different way to customise the palette and I'm missing it, so if someone knows a better way to customise MDL's palette I'm open to suggestions. Either way this solves the original question.
I'm trying to implement a MRT-DTI real-time fibertracking visualization tool based on VTK.
Therefore we need to read the DTI tensors/matrices per cell stored in a NIFTI Image (.nii) and I really can't figure out how to do this.
It's not a problem to retrieve a single scalar value from the NIFTI file, but I don't know how to get the tensor (3x3/4x4 matrix).
We would really appreciate any help !
Since the NIFTIImageReader is supposed to read a tensor NIFTI image as a multi-component vtkImage we tried this:
vtkSmartPointer<vtkImageExtractComponents> extractTupel1 = vtkSmartPointer<vtkImageExtractComponents>::New();
extractTupel1->SetInputConnection(reader->GetOutputPort());
extractTupel1->SetComponents(0,1,2);
extractTupel1->Update();
vtkSmartPointer<vtkImageExtractComponents> extractTupel2 = vtkSmartPointer<vtkImageExtractComponents>::New();
extractTupel2->SetInputConnection(reader->GetOutputPort());
extractTupel2->SetComponents(3, 4, 5);
extractTupel2->Update();
vtkSmartPointer<vtkImageExtractComponents> extractTupel3 = vtkSmartPointer<vtkImageExtractComponents>::New();
extractTupel3->SetInputConnection(reader->GetOutputPort());
extractTupel3->SetComponents(6, 7, 8);
extractTupel3->Update();
extractTupel1->GetOutput()->GetPoint(pointId, tupel1);
extractTupel2->GetOutput()->GetPoint(pointId, tupel2);
extractTupel3->GetOutput()->GetPoint(pointId, tupel3);
But it doesn't work. Maybe the GetPoint-Method is the wrong choice?
Please help :)
Answer by David Gobbi, really much thanks to him!:
No, the GetPoint() method will not return the tensor value. It will return the coordinates of the voxel. So vtkImageExtractComponents is not necessary here, either.
A vtkImageData always stores the voxel values as its "Scalars" array, even if the voxel values are not scalar quantities.
A simple (but inefficient way) to get the scalar values is this method:
GetScalarComponentAsDouble (int x, int y, int z, int component)
For each voxel, you would call this method 9 times with component = [0..8].
A much more efficient way of getting the tensors is to get the scalar array from the data, and then look up the tensors via the pointId:
reader->Update();
vtkDataArray *tensors = reader->GetOutput()->GetPointData()->GetScalars();
double tensor[9];
tensors->GetTuple(pointId, tensor);
This is orders of magnitude more efficient than GetScalarComponentAsDouble().
I can't grok the docs on d3's ordinal scales. The way I read it (and the way it works for linear scales, I feel I should be able to proceed like this:
color = d3.scale.ordinal();
color.domain([0, 100]); // Input is any percentage point 0-100
color.range([ // Output is a scale of colors from "bad" to "good"
'red','orange','blue','green'
]);
This doesn't give me the results I expect:
color(0); // "red". Ok, that makes sense
color(1); // "blue". Huh? This should be "red"
color(100); // "orange". Really? I'm confused. What's the range?
color.range(); //["red", "orange", "blue", "green"]. That looks right...
color.domain(); // [0,1,100]. Um...
It looks like it's treating inputs as discrete categorical values when I want to treat them like numbers.
The correct approach for mapping a range of numbers to discrete outputs is to use quantize. The difference wasn't clear to me and ordinal seemed intuitive. Figured it out now.
A working solution looks like this:
color = d3.scale.quantize();
color.domain([0, 100]);
color.range([
'red','orange','blue','green'
]);
color(0); // "red"
color(1); // "red"
color(99); // "green"
These links here helpful in figuring this out:
http://roadtolarissa.com/blog/2015/01/04/coloring-maps-with-d3/
What is the difference between d3.scale.quantize() and d3.scale.quantile()?
The approach to this is not exactly how it works. The domain you listed will point to 2 specific values in the range, the 2 first values - red and orange. Any other value that you add to the domain via color(n); will extend the domain array, eg. 1 is considered the 3rd index, therefore it is assigned the 3rd item in the range, if you were to call another item with color(n) you would get the 4th index. That is how the range method works.
I'm having a hard time understanding what is going on when I use this parameter of the blockproc function:
When I try a simple function
fun = #(block) mean(mean(block.data));
im4 = blockproc(im1,[BlockSize BlockSize],fun);
It works like a charm.
Now the same function but when blocks include border pixels:
fun = #(block) mean(mean(block.data));
im4 = blockproc(im1,[BlockSize BlockSize],fun, 'BorderSize', [BlockSize BlockSize]);
It returns an empty im4. No error message, just an empty value. Changing the 'TrimBorder' or 'PadPartialBlock' parameters did not do any change. Is there something I'm missing?
The function has a very simple logic. If a border of 2 pixels was added, it will remove two pixels after processing the block. Your function outputs a scalar, after removing the border an empty array remains.
You have to turn trimborder off:
im4 = blockproc(im1,[BlockSize BlockSize],fun,'BorderSize', [1 1],'TrimBorder',false);