Is there a way tor apply a random color to change the style of the selected points to that color

236 Views Asked by At

So I have a random color function that I want to apply to a selection of points but it gives me a random color for each point rather one random color for the selection of points.I know the selecting part works but I'm not sure how to make it call upon that random color generator only once per color. Here is what I have so far. Thanks for any advice/help!

include "mapbasic.def"
include "menu.def"
include "icons.def"

Declare Sub Main
Declare Sub Color
Declare Sub RndColor



Declare Function ChangeSymbolColour (ByVal objTarget as Object, ByVal stringAttribute as String) as Object

' ==========================

Sub Main

Dim i, nRecords as integer
Dim ColValue As Alias
Dim s_ColValues(50) as String


'// - Query your table and group wanted column (list of unique values)
Select Symbology from YOUR_TABLE2 group by Symbology into UNIQUE_values

'// - Populate array with all unique values (from first column in UNIQUE_values table)
print "new"  
  For i = 1 to SelectionInfo(SEL_INFO_NROWS)         
    ReDim s_ColValues(i)
    Fetch rec i From UNIQUE_values

    ColValue = UNIQUE_values + ".col1"

    s_ColValues(i) = ColValue

Select * from Your_Table2 where Symbology= s_ColValues(i) into Selection

Call Color

Next

End Sub

Sub RndColor

Dim Color as integer

Color = RGB((254*Rnd(1)+1),(254*Rnd(1)+1),(254*Rnd(1)+1))

End Sub

Sub Color


Update Your_table2 set Obj = ChangeSymbolColour(obj, symbology)


End Sub



Function ChangeSymbolColour (ByVal objTarget as Object, ByVal stringAttribute as String) as Object

Dim newSymbol as Symbol
Dim nColour as Integer
'nColour=RGB((254*Rnd(1)+1),(254*Rnd(1)+1),(254*Rnd(1)+1))
ncolour=RED

newSymbol = MakeFontSymbol(36, nColour, 5, "Map Symbols", 0, 0) 
Alter Object objTarget Info OBJ_INFO_SYMBOL, newSymbol
ChangeSymbolColour = objTarget
End Function
1

There are 1 best solutions below

1
On BEST ANSWER

You need to generate the random colour outside of the function and then pass it in as a parameter rather than generating a new colour every time the function is called. Also, you don't seem to be using the StringAttribute parameter of the function, is this really required?

e.g.

Sub Main

    Dim nColour as Integer
    
    ...
    Select * from MyTable where ... into MySelection
    nColour = GetRandomColour()
    Update MySelection Set Obj = ChangeSymbolColour(obj, nColour)

End Sub

Function GetRandomColour() as Integer

    GetRandomColour = RGB((254*Rnd(1)+1),(254*Rnd(1)+1),(254*Rnd(1)+1))

End Function

Function ChangeSymbolColour (ByVal objTarget as Object, ByVal nColour as Integer) as Object

    Dim newSymbol as Symbol

    newSymbol = MakeFontSymbol(36, nColour, 5, "Map Symbols", 0, 0) 
    Alter Object objTarget Info OBJ_INFO_SYMBOL, newSymbol
    ChangeSymbolColour = objTarget

End Function