impworks Logo - a grinning imp in flight

CityEngine Colour Combinations and String Multiplication

Sunday, January 16th, 2011

CityEngine Colour Combinations

Working on my CityEngine Constructivism WiP model today I wanted to simplify the way I’m colouring the models. Rather than just assigning random colours or fixed colours I’ve created some sets of colours for my buildings. I’ve got the set of colour combinations stored as a string that I’m picking one at random for each lot using listRandom

[codesyntax lang=”perl”]

const colourcombinations2= "#ffffff#ff0000;"+
			   "#ffffff#333399;"+
			   "#ffffff#cccccc;"+
		           "#cccccc#ffffff;"+
			   "#cccccc#ffff00;"+
			   "#ffff00#ff0000;"+
			   "#ff0000#ffffff;"+
			   "#333399#ff0000;"+
			   "#000000#ff0000;"+
			   "#000000#ffffff;"

attr buildingcolour2=listRandom(colourcombinations2)

[/codesyntax]

To pull a colour from the randomly selected combination I’ve created a simple function…

[codesyntax lang=”perl”]

ColourCode(Colours,CN)=substring(Colours,((CN-1)*8-CN+1),((CN*8)-CN))

[/codesyntax]

The first value given to ColourCode is the random colour string in buildingcolour2 and CN is the number of the colour I want. I’m using colour 1 for the top of each building and colour 2 for the sides so I can set them like this…
[codesyntax lang=”perl”]

Lot --> extrude (10) Building
Building --> comp(f) { side: Side | top: Top }
Top --> color (ColourCode(buildingcolour2,1))
Side --> color (ColourCode(buildingcolour2,2))

[/codesyntax]

If you want more than two colours in a combination simply add them into the string (eg  "#ffffff#ff0000#000000#0000ff;" would be four colours) and call them with ColourCode and the appropriate postion ( ColourCode(buildingcolour2,4) would get the fourth colour).  While my colour combinations are deliberately quite bold and unrealistic more subtle and realistic combinations can be created in just the same way.

Now I wanted to have some colour combinations turn up more often than others. I could just cut and paste them so they are in the string more often but thats slow, boring and likely to introduce errors. Now CityEngine’s string handling features are quite basic compared to say php or python. To be fair its not really what CityEngine is built to deal with. I wanted a string multiplying function but there isn’t one. So I built one myself…

[codesyntax lang=”perl”]

StrMultiply (ShortStr,Number)=StrLoopConcat(ShortStr,"",floor(Number))

StrLoopConcat (ShortStr,Str,Number)=  case Number > 1 : StrLoopConcat (ShortStr,Str+ShortStr,Number-1)
 else: Str

[/codesyntax]

So now my set up of the colour strings looks like this…

[codesyntax lang=”perl”]

const colourcombinations2=StrMultiply("#ffffff#ff0000;",10)+
StrMultiply ("#ffffff#333399;",10)+
StrMultiply ("#ffffff#cccccc;",10)+
StrMultiply ("#cccccc#ffffff;",1)+
StrMultiply ("#cccccc#ffff00;",2)+
StrMultiply ("#ffff00#ff0000;",2)+
StrMultiply ("#ff0000#ffffff;",4)+
StrMultiply ("#333399#ff0000;",2)+
StrMultiply ("#000000#ff0000;",2)+
StrMultiply ("#000000#ffffff;",2)

[/codesyntax]

A simple rules file can show both random selection of colour combinations and simple string multiplication off…

[codesyntax lang=”perl”]

/**
 * File:    rules.cga
 * Created: 16 Jan 2011 19:40:44 GMT
 * Author:  mark
 */

version "2010.3"

Street --> color ("#000000")
Sidewalk --> color ("#000000")
Crossing --> color ("#000000")
Junction --> color ("#000000")
JunctionEntry --> color ("#000000")
LotInner --> Lot

Lot --> extrude (rand (5,30)) Building
Building --> comp(f) { side: Side | top: Top }
Top --> color (ColourCode(buildingcolour2,1))
Side --> color (ColourCode(buildingcolour2,2))

attr buildingcolour2=listRandom(colourcombinations2)

ColourCode(Colours,CN)=substring(Colours,((CN-1)*8-CN+1),((CN*8)-CN))

StrMultiply (ShortStr,Number)=StrLoopConcat(ShortStr,"",floor(Number))
StrLoopConcat (ShortStr,Str,Number)= case Number > 1 : StrLoopConcat (ShortStr,Str+ShortStr,Number-1)
else: Str

const colourcombinations2=StrMultiply("#ffffff#ff0000;",10)+
StrMultiply ("#ffffff#333399;",10)+
StrMultiply ("#ffffff#cccccc;",10)+
StrMultiply ("#cccccc#ffffff;",1)+
StrMultiply ("#cccccc#ffff00;",2)+
StrMultiply ("#ffff00#ff0000;",2)+
StrMultiply ("#ff0000#ffffff;",4)+
StrMultiply ("#333399#ff0000;",2)+
StrMultiply ("#000000#ff0000;",2)+
StrMultiply ("#000000#ffffff;",2)

[/codesyntax]

CityEngine Colour Combinations

Version: 0.1
Size: 676.2 KB
January 16, 2011
Leave a Comment

impworks © Copyright Mark Caldwell 1996 - 2024