Results 1 to 3 of 3

Thread: Color Recognition

  1. #1

    Thread Starter
    Fanatic Member PsychoMark's Avatar
    Join Date
    Feb 2001
    Location
    Netherlands
    Posts
    540
    Here's some code, written in Delphi but easy to read and translate (just scream if you can't get it to work ):

    http://homepages.borland.com/efg2lab...Colors/HSV.htm
    Teaudirenopossum.Musasapientumfixaestinaure.
    (I can't hear you. There's a banana in my ear)

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I actually found a similar page that did this in C++ (almost EXACTLY the same!) I translated the code:
    VB Code:
    1. Function Max(R, G, B) As Single
    2.     If R > G Then
    3.         If R > B Then
    4.             Max = R
    5.         Else
    6.             Max = B
    7.         End If
    8.     Else
    9.         If G > B Then
    10.             Max = G
    11.         Else
    12.             Max = B
    13.         End If
    14.     End If
    15. End Function
    16.  
    17. Function Min(R, G, B) As Single
    18.     If R < G Then
    19.         If R < B Then
    20.             Min = R
    21.         Else
    22.             Min = B
    23.         End If
    24.     Else
    25.         If G < B Then
    26.             Min = G
    27.         Else
    28.             Min = B
    29.         End If
    30.     End If
    31. End Function
    32.  
    33. Function RGBtoHSB(R, G, B, H, S, V)
    34. Dim Mini As Single, Maxi As Single, Delta As Single
    35.    
    36.     R = BToS(R)
    37.     G = BToS(G)
    38.     B = BToS(B)
    39.    
    40.     Mini = Min(R, G, B)
    41.     Maxi = Max(R, G, B)
    42.    
    43.     V = Maxi
    44.     Delta = Maxi - Mini
    45.    
    46.     If Maxi <> 0 Then
    47.         S = Delta / Maxi
    48.     Else
    49.         S = 0
    50.         H = -1
    51.         Exit Function
    52.     End If
    53.  
    54.     If R = Maxi Then
    55.         H = (G - B) / Delta
    56.     ElseIf G = Maxi Then
    57.         H = 2 + (B - R) / Delta
    58.     Else
    59.          H = 4 + (R - G) / Delta
    60.     End If
    61.  
    62.     H = H * 60
    63.     If H < 0 Then H = H + 360
    64.  
    65.     S = S * 100
    66.     V = V * 100
    67.  
    68. End Function
    69. Function HSBtoRGB(R, G, B, H, S, V)
    70. Dim I As Integer
    71. Dim F As Single, P As Single, Q As Single, T As Single
    72.    
    73.     S = S / 100
    74.     V = V / 100
    75.    
    76.     If S = 0 Then
    77.         R = G = B = V
    78.         Exit Function
    79.     End If
    80.  
    81.     H = H / 60
    82.     I = Int(H)
    83.     F = H - I
    84.     P = V * (1 - S)
    85.     Q = V * (1 - S * F)
    86.     T = V * (1 - S * (1 - F))
    87.    
    88.     Select Case I
    89.         Case 0
    90.             R = V
    91.             G = T
    92.             B = P
    93.         Case 1
    94.             R = Q
    95.             G = V
    96.             B = P
    97.         Case 2
    98.             R = P
    99.             G = V
    100.             B = T
    101.         Case 3
    102.             R = P
    103.             G = Q
    104.             B = V
    105.         Case 4
    106.             R = T
    107.             G = P
    108.             B = V
    109.         Case Else
    110.             R = V
    111.             G = P
    112.             B = Q
    113.     End Select
    114.    
    115.     R = Round(R * 255)
    116.     G = Round(G * 255)
    117.     B = Round(B * 255)
    118.  
    119. End Function
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3

    Thread Starter
    Fanatic Member PsychoMark's Avatar
    Join Date
    Feb 2001
    Location
    Netherlands
    Posts
    540
    Well, the Delphi code has been translated from C code, so it's probably the same
    Teaudirenopossum.Musasapientumfixaestinaure.
    (I can't hear you. There's a banana in my ear)

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