Home > database >  Inter-related c struct's - how use color code to look in struct to get name
Inter-related c struct's - how use color code to look in struct to get name

Time:01-20

I'm trying to access the string color name for a color code found through structs. How do I get the string color name from the color code?

I've seen this done, but I wanted to show the string color name and not color code if I loop through all the items (it would know the color code it is so I shouldn't need to refer to the color by name and should be able to show the string color name:

_stprintf(tempMsg, _T("## InkRest Black = %d, InkDimention Black = %d"), pStatusStruct->InkRest.Black, pStatusStruct->InkDimention.Black);

This is what the code looks like:

void ClassName::getInk()
{
   ByteBuffer  buf = StatusBuffer;
   STATUS_02 pStatusStruct = (STATUS_02) & buf[0];
   TCHAR tempMsg[MAX_PATH * 2] = { 0 };
   for (int i = 0; i < sizeof(&pStatusStruct->CartInk); i  )
    {
        _stprintf(tempMsg, _T("## Ink Types(i = %d, InkRest = 0x%x,  ColorType = 0x%lx, ColorName = 0x%lx"), i, pStatusStruct->CartInk[i].InkRest,  pStatusStruct->CartInk[i].ColorType, pStatusStruct->CartInk[i].ColorType.stringnamehere); //the last one needs to return the string name ??

     }
 }

The Struct's are defined like this:

typedef struct _INKINFO {
    BYTE    Black ;                     //Black
    BYTE    Cyan ;                      //Cyan
    BYTE    Magenta ;                   //Magenta
    BYTE    Yellow ;                    //Yellow
    BYTE    LightCyan ;                 //Light cyan
    BYTE    LightMagenta ;              //Light magenta
    BYTE    LightYellow ;               //Light yellow
    BYTE    DarkYellow ;                //Dark yellow
    BYTE    LightBlack ;                //Light black
    BYTE    Red ;                       //Red
    BYTE    Violet ;                    //Violet
    BYTE    Clear ;                     //Clear
    BYTE    Other ;                     //Other
    BYTE    LightLightBlack ;           //LightLightBlack
    BYTE    Orange ;                    //Orange
    BYTE    Green ;                     //Green
    BYTE    ReserveColor[3] ;           //Reserve
}INKINFO, *LPINKINFO ;

typedef struct _CARTINKINFO {   //Cart and ink information
    BYTE CartType ;             //Cart name code
    DWORD ColorType ;                   //Cart color code
    BYTE InkRest ;                      //Ink rest information
    BYTE InkDimension ;                 //Ink dimension information
} CARTANDINKINFO, *LPCARTANDINKINFO ;

typedef struct  _INKSTATUS_02{      // INK status structure 02
    ...
    CARTANDINKINFO CartInk[16] ;
    ...                     // cart and ink information
    
}INKSTATUS_02, *STATUS_02 ;

Also, this may be more applicable to the changes made to the ColorType with the _02 round, and is not a struct like the other color struct, but I guess I could make it one:

Color             <color bit> 
Black             0x00000001 
Cyan              0x00000002 
Magenta           0x00000004 
Yellow            0x00000008 
Light cyan        0x00000010 
Light magenta     0x00000020 
Light yellow      0x00000040 
Dark yellow       0x00000080 
Light black       0x00000100 
Red               0x00000200 
Violet            0x00000400 
Clear             0x00000800 
Unknown           0x00001000 
Light Light Black 0x00002000 
Orange            0x00004000 
Green             0x00008000 
White             0x00010000 
Cleaning          0x00020000 
Silver            0x00040000 
Gray              0x00080000

I'm having trouble finding what to google for this question. I appreciate any ideas!

CodePudding user response:

You can use a map to look up a name for a color ID, something like this:

std::map<DWORD, std::string> colorNames {
    {0x00000001,  "Black" },
    {0x00000002,  "Cyan" },
    {0x00000004,  "Magenta" },
    {0x00000008,  "Yellow" },
    {0x00000010,  "Light cyan" },
    {0x00000020,  "Light magenta" },
    {0x00000040,  "Light yellow" },
    {0x00000080,  "Dark yellow" },
    {0x00000100,  "Light black" },
    {0x00000200,  "Red" },
    {0x00000400,  "Violet" },
    {0x00000800,  "Clear" },
    {0x00001000,  "Unknown" },
    {0x00002000,  "Light Light Black" },
    {0x00004000,  "Orange" },
    {0x00008000,  "Green" },
    {0x00010000,  "White" },
    {0x00020000,  "Cleaning" },
    {0x00040000,  "Silver" },
    {0x00080000,  "Gray" }
};

...which you'd use something like this:

for (int i = 0; i < sizeof(&pStatusStruct->CartInk); i  ) {
 
    auto name = colorNames.find(pStatusStruct->CartInk[i].ColorType);

    _stprintf(tempMsg, _T("## Ink Types(i = %d, InkRest = 0x%x,  ColorType = 0x%lx, ColorName = %s"), 
        i, 
        pStatusStruct->CartInk[i].InkRest,  
        pStatusStruct->CartInk[i].ColorType, 
        name->c_str());
}

You probably also should check that name != colorNames.end() before you try to use it--that's what'll happen if you pass a value that's not in the map. Presumably that should never happen, but ...

  • Related