2

How can I convert this C define macro to C#?

#define CMYK(c,m,y,k)       ((COLORREF)((((BYTE)(k)|((WORD)((BYTE)(y))<<8))|(((DWORD)(BYTE)(m))<<16))|(((DWORD)(BYTE)(c))<<24)))

I have been searching for a couple of days and have not been able to figure this out. Any help would be appreicated.

a3f
  • 8,517
  • 1
  • 41
  • 46
iAmMe
  • 71
  • 1
  • 5
  • 1
    Do you mean how can you pack 4 bytes in to one interger - or how can you create a C# Color struct from CMYK parameters? – James Gaunt Jun 03 '11 at 14:06

2 Answers2

5

C# doesn't support #define macros. Your choices are a conversion function or a COLORREF class with a converting constructor.

public class CMYKConverter
{
    public static int ToCMYK(byte c, byte m, byte y, byte k)
    {
        return k | (y << 8) | (m << 16) | (c << 24);
    }
}

public class COLORREF
{
    int value;
    public COLORREF(byte c, byte m, byte y, byte k)
    {
        this.value = k | (y << 8) | (m << 16) | (c << 24);
    }
}
David Yaw
  • 27,383
  • 4
  • 60
  • 93
  • 1
    it's also worth noting that COLORREF is intended to be a 32 bit RGB value. not sure what this code is doing but it's not a standard method of converting CMYK to rgb. http://stackoverflow.com/questions/2426432/convert-rgb-color-to-cmyk – madmik3 Jun 03 '11 at 14:11
2

C# does not support C/C++ like macros. There is no #define equivalent for function like expressions. You'll need to write this as an actual method of an object.

zellio
  • 31,308
  • 1
  • 42
  • 61