none
How to compare color RRS feed

  • Question

  • First i generate spread sheet excel file by EPPlus library where i set background color for few cell  like this way by EPPlus library.

    using (var range = worksheet.Cells[rownumber + 1, columnIndex + 1])
    {
        range.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
        range.Style.Fill.BackgroundColor.SetColor(Color.Olive);
        range.Style.ShrinkToFit = false;
    }
    


    after generating spread sheet by EPPlus the same file load into dev express spread sheet control. where i need to check cell background color is olive. i did it this way but the same code some time not working.

    //dgvReport is spreadsheet control of devexpress
    DevExpress.Spreadsheet.IWorkbook workbook = dgvReport.Document;
    DevExpress.Spreadsheet.Worksheet sheet = workbook.Worksheets[0];
    
    if (sheet[clickedRow, clickedColumn].Fill.BackgroundColor == Color.Olive)
    {
        sheet[clickedRow, clickedColumn].Fill.BackgroundColor = Color.Empty;
        sheet[clickedRow, clickedColumn].Borders.SetAllBorders(Color.Empty, DevExpress.Spreadsheet.BorderLineStyle.None);
    }



    so please tell me my above code where i am comparing color is right ? i first set background color by EPPlus and later comparing the color 
    by dev express spread sheet control like this way if (sheet[clickedRow, clickedColumn].Fill.BackgroundColor == Color.Olive) 

    is it right code ? if i am wrong then please rectify my code. 

    i am not asking any question specific to Devexpress. it is all about color. One guy said To compare two colors, it is better to use their ARGB representation (Color.ToArgb)

    https://stackoverflow.com/questions/20994753/compare-two-color-objects

    looking for a sample code where i will use ARGB representation for color comparision. please help

    Friday, March 1, 2019 8:54 AM

Answers

  • You can compare Color types as it implements the appropriate equality methods. The comparison should work just fine. But comparing colors to determine equality seems like a hack to me. You should really be comparing the underlying data, not what is shown in the UI. Use the data that determines whether the color should be Olive or not and compare that instead.

    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Sudip_inn Sunday, March 10, 2019 4:53 PM
    Friday, March 1, 2019 3:01 PM
    Moderator
  • Michael is right.  It's fine to compare to Color.Olive, but only if you KNOW that the cell you're looking for is always going to be exactly the same color that the C# Color.Olive constant returns.  Remember, there is no ISO standard for what RGB value maps to the name "olive".  There are many colors that look to the eye like "olive" that will not match bit for bit with the Color.Olive constant.  If you are reading cells that you created in the same program, it's probably fine, but if your cells have to be written out to an app or to a file and read back in, that's problematic.

    Michael is also right is saying that this is the wrong way to handle this.  You need to use some other criteria to recognize your target ranges.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by Sudip_inn Sunday, March 10, 2019 4:53 PM
    Saturday, March 2, 2019 1:42 AM

All replies

  • You can compare Color types as it implements the appropriate equality methods. The comparison should work just fine. But comparing colors to determine equality seems like a hack to me. You should really be comparing the underlying data, not what is shown in the UI. Use the data that determines whether the color should be Olive or not and compare that instead.

    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Sudip_inn Sunday, March 10, 2019 4:53 PM
    Friday, March 1, 2019 3:01 PM
    Moderator
  • Michael is right.  It's fine to compare to Color.Olive, but only if you KNOW that the cell you're looking for is always going to be exactly the same color that the C# Color.Olive constant returns.  Remember, there is no ISO standard for what RGB value maps to the name "olive".  There are many colors that look to the eye like "olive" that will not match bit for bit with the Color.Olive constant.  If you are reading cells that you created in the same program, it's probably fine, but if your cells have to be written out to an app or to a file and read back in, that's problematic.

    Michael is also right is saying that this is the wrong way to handle this.  You need to use some other criteria to recognize your target ranges.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by Sudip_inn Sunday, March 10, 2019 4:53 PM
    Saturday, March 2, 2019 1:42 AM
  • please give me sample code to compare color using ARGB representation

    how to convert Color,Olive to ARGB representation ?

    looking for code. thanks

    Monday, March 4, 2019 3:39 PM
  • Why? You don't need to compare the ARGB stuff because == is already doing that. Take the 2 color values that aren't working for you and compare their actual values. Then post your requests here. ToArgb

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, March 4, 2019 4:08 PM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find a solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, March 7, 2019 9:39 AM
    Moderator