Results 1 to 2 of 2

Thread: Color Values

  1. #1

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Color Values

    Howdy all,

    This may actually be more of a maths question so move it if need be. I'm wanting to know if anyone could provide me with an example of comparing two color values, at high speed. For example, say I have a Color Table, which has 20 colors in it. I want to get the value of a color, and check it against this table. If it comes close to another color in the table within a certain margin of error, then just change that color to the one in the table. If it doesn't, keep its color and add it to the table.

    I'm stumped as to how I would go about this, so any suggestions would be nice

    TIA,

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  2. #2
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    372

    Re: Color Values

    perhaps this will get you started-
    i just pounded this out online, so no guarantees, but you should be able to get the basic idea from this...





    VB Code:
    1. Option Explicit
    2.  
    3. Private Type Cols
    4.  
    5. r As Long
    6. g As Long
    7. b As Long
    8. End Type
    9.  
    10.  
    11. Dim colA As Cols
    12.   Dim colB As Cols
    13.  
    14.  
    15.  
    16.  
    17. Private Sub form_load()
    18.  
    19.   colA = longToRgb(Form1.BackColor)
    20.   colB = longToRgb(Text1.BackColor)
    21.  
    22.  If CompareCols(colA, colB) = True Then MsgBox "Colors roughly match" Else MsgBox "colors too far apart to match"
    23.  
    24. End Sub
    25.  
    26.  
    27. Private Function longToRgb(ByVal colLong As Long) As Cols
    28.     longToRgb.r = CLng((colLong And &HFF))
    29.     longToRgb.g = CLng(((colLong \ &H100&) And &HFF&))
    30.     longToRgb.b = CLng(((colLong \ &H10000) And &HFF&))
    31.  
    32. End Function
    33.  
    34.  
    35. Private Function CompareCols(col1 As Cols, col2 As Cols) As Boolean
    36. If col1.r < col2.r + 5 And col1.r > col2.r - 6 Then
    37.    If col1.g < col2.g + 5 And col1.g > col2.g - 6 Then
    38.       If col1.b < col2.b + 5 And col1.b > col2.b - 6 Then
    39.  
    40.              CompareCols = True ' values are within -5 or +6
    41.      
    42.       End If 'b
    43.    End If 'g
    44. End If 'r
    45.  
    46. End Function

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