Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET VS2012
I've made a color picker tool that gets a ARGB color from a pixel. I've figured out how to get HTML and HEX colors, but can't figure out how to get HSV or CMYK. All the articles that I've found are in C#. I've tried converting a few(with web converters), but I always run into a problem. Here's the code that I'm using to get the ARGB and HTML colors:
 
PXL_COLOR = bmp.GetPixel(0, 0).ToString 'ARGB
 
PXL_HTML = System.Drawing.ColorTranslator.ToHtml(bmp.GetPixel(0, 0)) 'HTML
 
Can someone please help?
Posted 8-Apr-13 3:05am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you have RGB you can calculate the CMYK and HSV values.
 
Have a look at this link: http://www.rapidtables.com/convert/color/index.htm[^]
 
Example:
R=50 = 50/255 = 0.196
G=100 = 100/255 = 0.392
B=150 = 150/255 = 0.588
 
K = 1-max(0.196, 0.392, 0.588) = 0.412
C = (1-0.196-0.412)/(1-0.412) = 0.667
M = (1-0.392-0.412)/(1-0.412) = 0.333
Y = (1-0.588-0.412)/(1-0.412) = 0
  Permalink  
v2
Comments
Marcus Kramer at 8-Apr-13 12:27pm
   
My 5. Good resource.
7774tlas at 8-Apr-13 15:08pm
   
Have patience w/ me, I'm horrible at math. This is what I've got so far, but what should I use in place of "max"? I keep getting an error.
 
Sub RGBtoCMYK()
 
Dim R As Double
Dim G As Double
Dim B As Double
Dim C As Double
Dim M As Double
Dim Y As Double
Dim K As Double
 
R = R / 255
G = G / 255
B = B / 255
 
K = 1 - max(R, G, B)
C = (1 - R - K) / (1 - K)
M = (1 - G - K) / (1 - K)
Y = (1 - B - K) / (1 - K)
 
End Sub
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

No Problem.
 
You can use Math.Max but it only accepts 2 values to compare. Max compares multiple values and retuns the largest one.
 
This should do the trick Smile | :)
Structure CMYK
        Dim C As Double
        Dim M As Double
        Dim Y As Double
        Dim K As Double
    End Structure
 
    Structure RGB
        Dim R As Double
        Dim G As Double
        Dim B As Double
    End Structure
 
    Private Function RGBtoCMYK(ByVal rgbIn As RGB) As CMYK
        Dim cmykOut As CMYK
        With rgbIn
            .R = .R / 255
            .G = .G / 255
            .B = .B / 255
        End With
        'math.max only accepts 2 aguments to compare. 
        'Put all the values in an array and get the .Max of the array
        Dim arr(2) As Double
        arr(0) = rgbIn.R
        arr(1) = rgbIn.G
        arr(2) = rgbIn.B
 
        With cmykOut
            .K = 1 - arr.Max
            .C = (1 - rgbIn.R - .K) / (1 - .K)
            .M = (1 - rgbIn.G - .K) / (1 - .K)
            .Y = (1 - rgbIn.B - .K) / (1 - .K)
        End With
        Return cmykOut
    End Function
You can call the funtion usings something like
Dim colorIn As RGB
   With colorIn
      .R = 50
      .G = 100
      .B = 150
   End With
Dim colorOut As CMYK = RGBtoCMYK(colorIn)
  Permalink  
Comments
7774tlas at 9-Apr-13 4:08am
   
I had not used functions or arrays before this, so not only have you solved my problem, but you also taught me a couple new tricks. Thank you very much for your time and knowledge.
petervanekeren at 10-Apr-13 5:55am
   
You're welcome :)
 
HSV is another story. Some serious (at least for me) math is needed to calculate that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 8 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100