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

  1. const colourcombinations2= "#ffffff#ff0000;"+
  2. "#ffffff#333399;"+
  3. "#ffffff#cccccc;"+
  4. "#cccccc#ffffff;"+
  5. "#cccccc#ffff00;"+
  6. "#ffff00#ff0000;"+
  7. "#ff0000#ffffff;"+
  8. "#333399#ff0000;"+
  9. "#000000#ff0000;"+
  10. "#000000#ffffff;"
  11.  
  12. attr buildingcolour2=listRandom(colourcombinations2)

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

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

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…

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

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…

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

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

  1. const colourcombinations2=StrMultiply("#ffffff#ff0000;",10)+
  2. StrMultiply ("#ffffff#333399;",10)+
  3. StrMultiply ("#ffffff#cccccc;",10)+
  4. StrMultiply ("#cccccc#ffffff;",1)+
  5. StrMultiply ("#cccccc#ffff00;",2)+
  6. StrMultiply ("#ffff00#ff0000;",2)+
  7. StrMultiply ("#ff0000#ffffff;",4)+
  8. StrMultiply ("#333399#ff0000;",2)+
  9. StrMultiply ("#000000#ff0000;",2)+
  10. StrMultiply ("#000000#ffffff;",2)

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

  1. /**
  2. * File: rules.cga
  3. * Created: 16 Jan 2011 19:40:44 GMT
  4. * Author: mark
  5. */
  6.  
  7. version "2010.3"
  8.  
  9. Street --> color ("#000000")
  10. Sidewalk --> color ("#000000")
  11. Crossing --> color ("#000000")
  12. Junction --> color ("#000000")
  13. JunctionEntry --> color ("#000000")
  14. LotInner --> Lot
  15.  
  16. Lot --> extrude (rand (5,30)) Building
  17. Building --> comp(f) { side: Side | top: Top }
  18. Top --> color (ColourCode(buildingcolour2,1))
  19. Side --> color (ColourCode(buildingcolour2,2))
  20.  
  21. attr buildingcolour2=listRandom(colourcombinations2)
  22.  
  23. ColourCode(Colours,CN)=substring(Colours,((CN-1)*8-CN+1),((CN*8)-CN))
  24.  
  25. StrMultiply (ShortStr,Number)=StrLoopConcat(ShortStr,"",floor(Number))
  26. StrLoopConcat (ShortStr,Str,Number)= case Number > 1 : StrLoopConcat (ShortStr,Str+ShortStr,Number-1)
  27. else: Str
  28.  
  29. const colourcombinations2=StrMultiply("#ffffff#ff0000;",10)+
  30. StrMultiply ("#ffffff#333399;",10)+
  31. StrMultiply ("#ffffff#cccccc;",10)+
  32. StrMultiply ("#cccccc#ffffff;",1)+
  33. StrMultiply ("#cccccc#ffff00;",2)+
  34. StrMultiply ("#ffff00#ff0000;",2)+
  35. StrMultiply ("#ff0000#ffffff;",4)+
  36. StrMultiply ("#333399#ff0000;",2)+
  37. StrMultiply ("#000000#ff0000;",2)+
  38. StrMultiply ("#000000#ffffff;",2)

CityEngine Colour Combinations

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

impworks © Copyright Mark Caldwell 1996 - 2018