Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to generate a series of unique HEX colors in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Module UniqueHexColors

    ' Convert an integer (0–255) to a two-digit HEX string.
    Function ToHex(value As Integer) As String
        Return value.ToString("X2").ToLower()
    End Function

    ' Generate N unique random HEX colors.
    Function GenerateRandomUniqueHexColors(count As Integer) As String()
        Dim seen As New HashSet(Of String)()
        Dim colors(count - 1) As String
        Dim rnd As New Random()

        Dim generated As Integer = 0

        While generated < count
            Dim r As Integer = rnd.Next(0, 256)
            Dim g As Integer = rnd.Next(0, 256)
            Dim b As Integer = rnd.Next(0, 256)

            Dim hex As String = "#" & ToHex(r) & ToHex(g) & ToHex(b)

            If seen.Add(hex) Then
                colors(generated) = hex
                generated += 1
            End If
        End While

        Return colors
    End Function

    Sub Main()
        Dim n As Integer = 12
        Dim colors = GenerateRandomUniqueHexColors(n)

        Console.WriteLine("Generated HEX colors:")
        For Each c In colors
            Console.WriteLine(c)
        Next
    End Sub

End Module


'
' run:
'
' Generated HEX colors:
' #03e686
' #e9d60c
' #00cba7
' #6f926d
' #0a33b7
' #a4d930
' #48d4fe
' #fab3fb
' #1ba2d8
' #553216
' #d88f09
' #1eb30f
'

 



answered 1 day ago by avibootz
...