Results 1 to 4 of 4

Thread: [RESOLVED] Read custom colors from a file , then load custom colors into color dialog box.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    874

    Resolved [RESOLVED] Read custom colors from a file , then load custom colors into color dialog box.

    Code:
      Dim ColorDialog1 As New ColorDialog
      Dim cMyCustomColors(16) As Color
    
      'read custom colors out of file and load into custom color array
      If IO.File.Exists(My.Application.Info.DirectoryPath & "\customcolors2.ini") Then
          ' Step 1: read lines from the file.
          For Each line In File.ReadLines(My.Application.Info.DirectoryPath & "\customcolors2.ini")
              ' Step 2: separate each line on the comma char.
              Dim parts = line.Split(","c)
              ' Step 3: print out the parts of the line.
              For Each value In parts
                  cMyCustomColors(parts(0)) = Color.FromArgb(parts(1), parts(2), parts(3)) 'RGB color values
              Next
          Next
      End If
    
      'Convert colors to integers
      Dim colorBlue As Integer
      Dim colorGreen As Integer
      Dim colorRed As Integer
      Dim iMyCustomColor As Integer
      Dim iMyCustomColors(cMyCustomColors.Length - 1) As Integer
    
      For index = 0 To cMyCustomColors.Length - 1
          'cast to integer
          colorBlue = cMyCustomColors(index).B
          colorGreen = cMyCustomColors(index).G
          colorRed = cMyCustomColors(index).R
          'shift the bits
          iMyCustomColor = colorBlue << 16 Or colorGreen << 8 Or colorRed
          iMyCustomColors(index) = iMyCustomColor
      Next
    
      ColorDialog1.CustomColors = iMyCustomColors
      
     If ColorDialog1.ShowDialog() = DialogResult.OK Then
        ' Me.BackColor = ColorDialog1.Color
    End If
    The file values for all 16 boxes as index, RGB values in comma delimited file called 'customcolors2.ini'

    Code:
    0,139,155,184
    1,188,213,254
    2,115,172,183
    3,200,249,198
    4,189,194,253
    5,200,249,255
    6,108,213,210
    7,236,164,236
    8,40,181,124
    9,126,107,203
    10,189,87,162
    11,65,77,160
    12,143,143,143
    13,255,255,255
    14,255,255,255
    15,155,155,155
    Screenshot attached as the result
    Attached Images Attached Images  
    Last edited by sdowney1; Jun 16th, 2024 at 07:12 AM.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    874

    Re: Read custom colors from a file , then load custom colors into color dialog box.

    Next, I need to know how to save the custom colors to that file.
    If anyone wants to show me that, thanks

    Looks to be an array of integers, which would be converted to RGB values

    Will ask question in the forum
    Code:
    ?ColorDialog1.CustomColors
    {Length=16}
        (0): 12098443
        (1): 16700860
        (2): 12037235
        (3): 13040072
        (4): 16630461
        (5): 16775624
        (6): 13817196
        (7): 15508716
        (8): 8172840
        (9): 13331326
        (10): 10639293
        (11): 10505537
        (12): 9408399
        (13): 16777215
        (14): 16777215
        (15): 10197915
    
    ?ColorDialog1.CustomColors(0)
    12098443
    Editing

    Ok, I got it, finally!
    This gives me the RGB values

    What is the A attribute?
    Except I don't need it AFAIK.

    Easy enough then to output all the custom colors back to a file, once you have the RGB values of the custom colors.

    Code:
            For xx = 0 To 15
                colorBlue = Color.FromArgb(ColorDialog1.CustomColors(xx)).B
                colorGreen = Color.FromArgb(ColorDialog1.CustomColors(xx)).G
                colorRed = Color.FromArgb(ColorDialog1.CustomColors(xx)).R
                'colorwwww = Color.FromArgb(ColorDialog1.CustomColors(xx)).A
            Next
    Last edited by sdowney1; Jun 15th, 2024 at 03:34 PM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    874

    Re: [RESOLVED] Read custom colors from a file , then load custom colors into color di

    This is my code for a public sub to the color of something, whatever control you want
    Colornumber is the color
    Boolcancel is if a user hits the cancel button
    Owner, I don't need at all.


    Code:
    'colornumber is the color chosen,
    'boolcancel user pressed the cancel button,
    'owner is who called the dialog.
    Public Sub modNumberColor(ByRef Colornumber As Color, ByRef BoolCancel As Boolean, ByRef Owner As Integer)
    
    	Dim ColorDialog1 As New ColorDialog
    	Dim cMyCustomColors(16) As Color
    	'Dim XX As Short = 0
    
    	'cc.hWndOwner = Owner
    	'read custom colors out of file and load into custom color array
    	If IO.File.Exists(My.Application.Info.DirectoryPath & "\customcolors2.ini") Then
    		' Step 1: read lines from the file.
    		For Each line In File.ReadLines(My.Application.Info.DirectoryPath & "\customcolors2.ini")
    			' Step 2: separate each line on the comma char.
    			Dim parts = line.Split(","c)
    			' Step 3: print out the parts of the line.
    			For Each value In parts
    				cMyCustomColors(parts(0)) = Color.FromArgb(parts(1), parts(2), parts(3)) 'RGB color values
    			Next
    		Next
    	End If
    
    	'Convert colors to integers
    
    	Dim colorBlue As Integer
    	Dim colorGreen As Integer
    	Dim colorRed As Integer
    	Dim iMyCustomColor As Integer
    	Dim iMyCustomColors(cMyCustomColors.Length - 1) As Integer
    
    	For index = 0 To cMyCustomColors.Length - 1
    		'cast to integer
    		colorRed = cMyCustomColors(index).R
    		colorGreen = cMyCustomColors(index).G
    		colorBlue = cMyCustomColors(index).B
    		'shift the bits
    		iMyCustomColor = colorBlue << 16 Or colorGreen << 8 Or colorRed
    		iMyCustomColors(index) = iMyCustomColor
    	Next
    
    	ColorDialog1.CustomColors = iMyCustomColors
    	If ColorDialog1.ShowDialog() = DialogResult.OK Then
    		BoolCancel = False
    		Colornumber = ColorDialog1.Color
    		'Dim c As Color = ColorDialog1.Color
    		'Dim valuec As Integer = ColorDialog1.Color.ToArgb()
    		'Colornumber = ColorDialog1.Color.ToArgb()
    
    	Else
    		'user hit cancel
    		' this will tell calling sub to exit and not set the color
    		BoolCancel = True
    
    		'Dim c As Color = ColorDialog1.Color
    		'Dim valuec As Integer = c.ToArgb()
    		'Label7.ForeColor = ColorTranslator.FromOle(valuec)
    
    	End If
    
    	'write these back to customcolors2.ini
    	'If IO.File.Exists(My.Application.Info.DirectoryPath & "\customcolors2.ini") Then Kill(My.Application.Info.DirectoryPath & "\customcolors2.ini")
    	Using wr = New StreamWriter(My.Application.Info.DirectoryPath & "\customcolors2.ini", False)
    		For XX = 0 To 15
    			colorBlue = Color.FromArgb(ColorDialog1.CustomColors(XX)).B
    			colorGreen = Color.FromArgb(ColorDialog1.CustomColors(XX)).G
    			colorRed = Color.FromArgb(ColorDialog1.CustomColors(XX)).R
    			'colorwwww = Color.FromArgb(ColorDialog1.CustomColors(xx)).A??
    			wr.WriteLine(String.Format("{0},{1},{2},{3}", XX, colorBlue, colorGreen, colorRed))
    		Next
    		wr.Flush()
    	End Using
    
    End Sub
    This is what comes before calling sub? for the public sub.

    Anyhow the whole thing has been working well.
    I have extra unused lines in here as part of my testing.

    Private Colorstuff as color is the dim statement.

    Label1.forecolor = Colorstuff will assign the colordialog.color to a control, etc...


    Code:
        Private Sub ColorView()
            On Error GoTo errhandler
            If frmlogonlog = False Then BoolCancel = True : Exit Sub 'Not authorized To change color
            Owner_Renamed = Me.Handle.ToInt32
            modNumberColor(Colorstuff, BoolCancel, Owner_Renamed) 'module2
    
    
            If BoolCancel = True Then Exit Sub
            'Colorstuff = Colornumber.Color.fromArgb()
            'Colorstuff = ColorTranslator.FromOle(Colornumber)
            'CommonDialog1Color.Color = ColorTranslator.FromOle(Colornumber)
            Exit Sub
    errhandler:
            'color is not right has to be assigned
        End Sub
    This sub on a right click of Label1, will run colorview, which runs the public sub to open the colordialog
    The value is returned in var Colorstuff and assigned to the Label1.forecolor
    Code:
        Private Sub Label1_MouseUp(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseUp
            'search the catalog
    
            If eventArgs.Button = MouseButtons.Right Then
                ColorView()
                If BoolCancel = True Then BoolCancel = False : Exit Sub
                ' Set the form's background color to selected color
                Label1.ForeColor = Colorstuff
    
                Exit Sub
            End If
    
        End Sub
    And that's the whole code needed right there.
    It is how I did it.

    All I need to do now is deal with if the file customcolors2.ini does not exist.
    The public sub will always write a new file of all 16 custom colors

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    874

    Re: [RESOLVED] Read custom colors from a file , then load custom colors into color di

    Ok, here is the complete module including TRY error handling and all dims
    I needed to add that incase the custom color file is badly hosed.

    If the file is wiped and totally rebuilt, of course all custom colors will be gone. But if it that messed up, can't be used.
    Can just restore from a backup file too.

    On a catch, it will run a colordialog box to get a color without all the custom color coding loading.
    Subsequent color dialogs will load that file with no custom colors as in they will be all the same, which a user would have to reestablish those colors.
    Which they can do in the catch block.

    Code:
    Option Strict Off
    Option Explicit On
    Imports System.IO
    Module Module2
    
    	Private CustomColors() As Byte
    	'colornumber is the color chosen,
    	'boolcancel user pressed the cancel button,
    	'owner is who called the dialog. Here not needed.
    	Public Sub modNumberColor(ByRef Colornumber As Color, ByRef BoolCancel As Boolean, ByRef Owner As Integer)
    
    		Try
    
    			Dim ColorDialog1 As New ColorDialog
    			Dim cMyCustomColors(16) As Color
    			Dim colorBlue As Integer
    			Dim colorGreen As Integer
    			Dim colorRed As Integer
    
    			'read custom colors out of file and load into custom color array
    			If IO.File.Exists(My.Application.Info.DirectoryPath & "\customcolors2.ini") Then
    				' Step 1: read lines from the file.
    				For Each line In File.ReadLines(My.Application.Info.DirectoryPath & "\customcolors2.ini")
    					' Step 2: separate each line on the comma char.
    					Dim parts = line.Split(","c)
    					' Step 3: print out the parts of the line.
    					For Each value In parts
    						cMyCustomColors(parts(0)) = Color.FromArgb(parts(1), parts(2), parts(3)) 'RGB color values
    					Next
    				Next
    
    				'Convert colors to integers
    
    				Dim iMyCustomColor As Integer
    				Dim iMyCustomColors(cMyCustomColors.Length - 1) As Integer
    
    				For index = 0 To cMyCustomColors.Length - 1
    					'cast to integer
    					colorRed = cMyCustomColors(index).R
    					colorGreen = cMyCustomColors(index).G
    					colorBlue = cMyCustomColors(index).B
    					'shift the bits
    					iMyCustomColor = colorBlue << 16 Or colorGreen << 8 Or colorRed
    					iMyCustomColors(index) = iMyCustomColor
    				Next
    
    				ColorDialog1.CustomColors = iMyCustomColors
    
    			End If
    
    			If ColorDialog1.ShowDialog() = DialogResult.OK Then
    				BoolCancel = False
    				Colornumber = ColorDialog1.Color
    			Else
    				'user hit cancel, this will tell calling sub to exit and not set the color
    				BoolCancel = True
    				'Dim c As Color = ColorDialog1.Color
    				'Dim valuec As Integer = c.ToArgb()
    
    			End If
    
    			'write these back to customcolors2.ini
    			'If IO.File.Exists(My.Application.Info.DirectoryPath & "\customcolors2.ini") Then Kill(My.Application.Info.DirectoryPath & "\customcolors2.ini")
    			Using wr = New StreamWriter(My.Application.Info.DirectoryPath & "\customcolors2.ini", False)
    				For XX = 0 To 15
    					colorBlue = Color.FromArgb(ColorDialog1.CustomColors(XX)).B
    					colorGreen = Color.FromArgb(ColorDialog1.CustomColors(XX)).G
    					colorRed = Color.FromArgb(ColorDialog1.CustomColors(XX)).R
    					'colorwwww = Color.FromArgb(ColorDialog1.CustomColors(xx)).A??
    					wr.WriteLine(String.Format("{0},{1},{2},{3}", XX, colorBlue, colorGreen, colorRed))
    				Next
    				wr.Flush()
    			End Using
    
    		Catch ex As Exception
    			'do it again as the file could be badly mangled
    			If IO.File.Exists(My.Application.Info.DirectoryPath & "\customcolors2.ini") Then Kill(My.Application.Info.DirectoryPath & "\customcolors2.ini")
    			'if an error boolcancel TRUE, then message, then try open again but no file involved
    			BoolCancel = True
    			Dim msg = ex.ToString()
    			MsgBox(msg,  , "Error")
    
    			Dim ColorDialog1 As New ColorDialog
    			Dim colorBlue As Integer
    			Dim colorGreen As Integer
    			Dim colorRed As Integer
    			If ColorDialog1.ShowDialog() = DialogResult.OK Then
    				BoolCancel = False
    				Colornumber = ColorDialog1.Color
    			Else
    				'user hit cancel, this will tell calling sub to exit and not set the color
    				BoolCancel = True
    			End If
    
    			'write these back to customcolors2.ini
    			Using wr = New StreamWriter(My.Application.Info.DirectoryPath & "\customcolors2.ini", False)
    				For XX = 0 To 15
    					colorBlue = Color.FromArgb(ColorDialog1.CustomColors(XX)).B
    					colorGreen = Color.FromArgb(ColorDialog1.CustomColors(XX)).G
    					colorRed = Color.FromArgb(ColorDialog1.CustomColors(XX)).R
    					'colorwwww = Color.FromArgb(ColorDialog1.CustomColors(xx)).A??
    					wr.WriteLine(String.Format("{0},{1},{2},{3}", XX, colorBlue, colorGreen, colorRed))
    				Next
    				wr.Flush()
    			End Using
    		End Try
    	End Sub
    
    End Module
    Last edited by sdowney1; Jun 17th, 2024 at 06:36 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width