Colorset with imperative, how?

Hello,

I know there are several ways … BUT i try this so how do i set my imperative that i get for each value a color ?

a;
_Farbe1;
_Farbe2;
_Farbe3;
_Farbe4;
_Farbe5;


y = [Imperative]
{
	if(a == 0.5) {
		return _Farbe1;
	}
	elseif (a == 1.0) {
		return _Farbe2;
	}
	elseif (a == 1.5) {
		return _Farbe3;
	}
	elseif (a == 2.25) {
		return _Farbe4;
	}
	else {
		return _Farbe5;
	}
};

I dont know much about imperative but in this example are you getting just 1 result because you are telling it to return the first true result rather than adding it to a list of results.

At a guess, maybe something like this instead?


a;
_Farbe1;
_Farbe2;
_Farbe3;
_Farbe4;
_Farbe5;

result = [Imperative] {
    result = [];

    if (a == 0.5) {
        result.append(1);
    }
    elseif (a == 1.0) {
        result.append(2);
    }
    elseif (a == 1.5) {
        result.append(3);
    }
    elseif (a == 2.25) {
        result.append(4);
    }
    else {
        result.append(5);
    }

    return result;
};
3 Likes

If you want to work with lists you have to iterate over the items.
You are treating a as an item but a is a list, that can´t work out.

aList; // List of radiuses, e.g., [0.5, 1.0, 2.25]
_Farbe1;
_Farbe2;
_Farbe3;
_Farbe4;
_Farbe5;

y = [Imperative]
{
    results = [];  // List to hold results for each radius

    for(a in aList) {  // Loop through each radius in the list
        if(a == 0.5) {
            results.append(_Farbe1);
        }
        elseif (a == 1.0) {
            results.append(_Farbe2);
        }
        elseif (a == 1.5) {
            results.append(_Farbe3);
        }
        elseif (a == 2.25) {
            results.append(_Farbe4);
        }
        else {
            results.append(_Farbe5);
        }
    }

    return results;  // Return the list of results
};
4 Likes

Hi, with a dictionary approach

a;
_Farbe1;
_Farbe2;
_Farbe3;
_Farbe4;
_Farbe5;

dic={"0.5":_Farbe1,
"1.0":_Farbe2,
"1.5":_Farbe3,
"2.25":_Farbe4,
"Other":_Farbe5};

a==0.5?Dictionary.ValueAtKey(dic,"0.5"):
a==1.0?Dictionary.ValueAtKey(dic,"1.0"):
a==1.5?Dictionary.ValueAtKey(dic,"1.5"):
a==2.25?Dictionary.ValueAtKey(dic,"2.25"):
Dictionary.ValueAtKey(dic,"Other");

cordially
christian.stan

2 Likes

Working with lists in Imperative is different from Associative. In Associative you work with lists as a whole (with lacing taking care of the manner in which elements between lists are computed). In Imperative you step through list elements individually.

spheres = Sphere.ByCenterPointRadius(Point.ByCoordinates(0..#4..4),0.5..#4..0.5);

coloredSpheres = [Imperative]
{
	s = [];
	i = 0;
	for (sphere in spheres)
	{
		if (sphere.Radius == 0.5)
			s[i] = GeometryColor.ByGeometryColor(sphere,Color.ByARGB(255,255,0,0));

		elseif (sphere.Radius == 1.0)
			s[i] = GeometryColor.ByGeometryColor(sphere,Color.ByARGB(255,0,255,0));

		elseif (sphere.Radius == 1.5)
			s[i] = GeometryColor.ByGeometryColor(sphere,Color.ByARGB(255,0,0,255));

		else
			s[i] =  GeometryColor.ByGeometryColor(sphere,Color.ByARGB(255,255,255,0));
		i = i + 1;
	}
	return s;
};
4 Likes