// Index based Randomizer
// IdxRndCol.osl, by Zap Andersson
// Modified: 2020-06-12
// Copyright 2019 Autodesk Inc, All rights reserved. This file is licensed under Apache 2.0 license
//    https://github.com/ADN-DevTech/3dsMax-OSL-Shaders/blob/master/LICENSE.txt

shader IdxRndCol 
[[ string help  = "Return a randomized color based on an input integer index",
   string category = "Math Color",
   string label = "Random by Index (color)",
   string version = "2.0" ]]
(
	int   Idx   = 0,
	int   Seed  = 1,
	color Min   = 0.0,
	color Max   = 1.0,
	int   PerComponent = 0 
		[[ string widget = "checkBox", int connectable = 0, 
		   string label  = "Randomize Per Component.",
		   string help = "When on, randomizes each R, G and B component individually. "
		                 "When off, interpolates between the Min and Max colors as a whole." ]],	
	output color Out = 0.0
)
{
	// abs() to deal w. the versioning issue w. negative cellnoise values
	
	if (PerComponent)
	{
		Out = mix(Min, Max, noise("cell", vector(abs(Idx), abs(Seed), 10)));
	}
	else
	{
		float ratio = noise("cell", vector(abs(Idx), abs(Seed), 10)); 
		Out = mix(Min, Max, ratio);
	}
}
