Cast Object to a Color

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cyclonit
    New Member
    • Dec 2011
    • 3

    #1

    Cast Object to a Color

    Hi,

    I'm currently working on a function which reads an xml file and automatically checks whether the information in this file may be used or not. If it finds a row which cannot be used it will automatically replace this row by a default row which is hardcoded into the program.

    My problem is, that I don't want to put the color codes as strings into my DataTable but convert them to actual Colors right away. This succeeds but when I try to cast the color out of the DataTable it fails and I don't see why because I have seen this in several examples.

    The program does the following:
    String -> Hex -> Color -> Save into DataTable -> Cast as Color

    and the last step fails. Does anyone know why and how I can fix this?

    best regards
    Cyclonit
    Last edited by Cyclonit; Dec 7 '11, 10:46 AM. Reason: misspelling
  • adriancs
    New Member
    • Apr 2011
    • 122

    #2
    show the code where the error occur

    Comment

    • Cyclonit
      New Member
      • Dec 2011
      • 3

      #3
      I'm doing the following to write the color into the DataTable:

      Code:
      String colorHex = (String)fileRow.ItemArray[3];
      colorHex = colorHex.TrimStart('#');
      
      Int32 R = Int32.Parse(colorHex.Substring(0, 2), NumberStyles.AllowHexSpecifier);
      Int32 G = Int32.Parse(colorHex.Substring(2, 2), NumberStyles.AllowHexSpecifier);
      Int32 B = Int32.Parse(colorHex.Substring(4, 2), NumberStyles.AllowHexSpecifier);
      
      Color color = Color.FromArgb(255, R, G, B);
      Object colorObj = color;
      
      tempDesign.Rows.Add((String)fileRow.ItemArray[0], (String)fileRow.ItemArray[1], (String)fileRow.ItemArray[2], colorObj);
      When I check how the entry looks like in the DataTable (via "run to cursor") VisualStudio shows it like this:
      Code:
      colorObj = "{Name=ffff00aa, ARGB=(255, 255, 0, 170)}"
      My code to extract it (not final, just for debugging) is:
      Code:
      Object colorObj = SMDesign.Design.Rows[0].ItemArray[3];
      Color color = (Color)colorObj;
      And VisualStudio shows the object this way:
      Code:
      colorObj = "Color [A=255, R=255, G=0, B=170]"

      Comment

      • adriancs
        New Member
        • Apr 2011
        • 122

        #4
        well, I have tried storing and retrieving color value in and from a DataTable with the following code:

        Code:
        DataTable dt = new DataTable();
        dt.Columns.Add("color", typeof(Color));
        dt.Rows.Add(Color.FromArgb(255,123,132,132));
        dt.Rows.Add(Color.Red);
        dt.Rows.Add(Color.FromName("yellow"));
        Color a = (Color)dt.Rows[0][0];
        Color b = (Color)dt.Rows[1][0];
        Color c = (Color)dt.Rows[2][0];
        Is this solve your problem?

        Comment

        • Cyclonit
          New Member
          • Dec 2011
          • 3

          #5
          Yes thank you very much.

          Comment

          • adriancs
            New Member
            • Apr 2011
            • 122

            #6
            Hi, you are welcome. perhaps, you may consider to mark "choose as best answer button" on the most appropriate post that has solved your problems. :-)

            Comment

            Working...