ColorTransferFunction - set alpha for a single point

Hello,

I am currently using vtkColorTransferFunction together with a preset colormap to color points, which very convenient when processing all points as a whole.
However, I’m trying to implement a “focus” feature, which consists of highlighting a selected point by make all the others transparent. Therefore, changing their alpha value.
From what I could find in this forum and Github, ColorTransferFunction does not support RGBA, but only RGB.

Is there any way I could change the Alpha value for a single point while still using ColorTransferFunction?
If not, is there any workaround for it?

Cheers,
Zaid

IDK if there’s a way to do it with ColorTransferFunction, I think not.

But the main way to vary opacity, I think, is to create a PiecewiseFunction and use the poorly named setPiecewiseFunction on the Property. Not seeing where this is documented, but it seems to work.
So something like this:

const ofun = vtkPiecewiseFunction.newInstance();
ofun.addPoint(200.0, 0.0);
ofun.addPoint(1200.0, 0.2);
ofun.addPoint(4000.0, 0.4);
actor.getProperty().setPiecewiseFunction(0, ofun);

Right.

It also looks like setPiecewiseFunction is not available for all actors. This can be used with volumeViewer, but not with a simple actor (which I’m using).

Furthermore, a PiecewiseFunction does not provide individual point control, but applies it on all the set of points.

If it’s a polydata, you use a vtkLookupTable.newInstance() (that takes RGBA) as Mapper.setLookupTable()

Is it possible to use vtkColorMaps just like you would use it for vtkColorTransferFunction by using applyColorMap? I could not find that either.

I guess it is missing indeed.
You can add vtkLookupTable.applyColorMap() yourself (contributions are welcome :slight_smile: ) looking at how it has been done in vtkColorTransferFunction.applyColorMap. I guess it will only apply to “IndexedColors” color maps.

I have a method where I map from a Qt QGradient instance to a vtkLookupTable by way of a vtkColorTransferFunction. The QGradient has a set of color stops which in a Qt application results in color interpolation between stops. To do the same in vtk, I do this to create the lut:

vtkSmartPointer<vtkLookupTable> CQVTKGradientBuilder::createFromGradient( const QGradient & gradient, int nLevels /*= 256 */ )
{
	vtkNew<vtkColorTransferFunction> ctf;
	ctf->SetColorSpaceToRGB();

	const QGradientStops & stops = gradient.stops();
	for ( auto & stop : stops )
	{
		auto	position = stop.first;
		auto	color = stop.second;

		ctf->AddRGBPoint( position, color.redF(), color.greenF(), color.blueF() );
	}

	vtkNew<vtkLookupTable> lut;
	lut->SetNumberOfTableValues( nLevels );
	lut->Build();

	double rgb[ 3 ];
	auto	nColors = lut->GetNumberOfColors();
	for ( auto nColor = 0; nColor < nColors; ++nColor )
	{
		ctf->GetColor( static_cast<double>( nColor ) / nColors, rgb );
		lut->SetTableValue( static_cast<vtkIdType>( nColor ), rgb[0], rgb[1], rgb[2], 1.0 );
	}
	return lut;
}

Perhaps you can use this with vtkLookupTable::GetColorValue() / SetColorValue() to change the opacity on the fly.