This project is read-only.

shape random colors

Oct 13, 2015 at 9:06 AM
It might just be impossible...

I was wondering if I can give every shape a different (random) color without creating 100 categories first. Tried something like this:
Dim red as Integer
Dim green as Integer
Dim blue as Integer

red = 255
green = 0
blue = 0

Then in a loop working on a shapefile called sf everytime a new shape is being created:
'change colors by sort of random
red = (red + 50) Mod 255
green = (green + 78) Mod 255
blue = (blue + 134) Mod 255
sf.DefaultDrawingOptions.LineColor = RGB(red, green, blue)

Somebody could predict I guess, but all shapes now will get the last created color. But on a single shape I cannot fiend any properties for the color.

Anybody ideas if something like this is possible without creating a new category for every single shape?

Thanks in advance!
Oct 14, 2015 at 9:22 AM
In case somebody is looking at the same, I solved this with a procedure creating the categories (VBA):
Sub CreateLineColors(sf as MapWINGIS.Shapefile, aantal As Integer)

Dim ct As MapWinGIS.ShapefileCategory
Dim i As Integer

For i = 0 To aantal - 1
    Set ct = sf.Categories.Add("Kleur" & i)
    ct.DrawingOptions.LineColor = RGB(RandomColor(225), RandomColor(225), RandomColor(225))
    ct.DrawingOptions.LineWidth = 3
Next i

End Sub

Function RandomColor(MaxValue As Integer) As Integer
    RandomColor = Int((MaxValue * Rnd) + 1)
End Function
(because I don't want almost white colours I maximized the values to 225)

Now after creating the shapefile I call the function with the variables holding my shapefile and the number of colours I want (NrColors). Every shape will now get a category by the following:
shpIndex = sf.EditAddShape(shp)
If shpIndex = -1 Then
    'Something goes wrong!
    sf.ShapeCategory(shpIndex) = shpIndex Mod NrColors
End If