Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. function getCellColor()
  2. clear all;
  3. clc;
  4.  
  5. % Excel
  6. filename = 'colorTest.xlsx';
  7.  
  8. % Start Excel as ActiveX server process on local host
  9. Excel = actxserver('Excel.Application');
  10.  
  11. % Handle requested Excel workbook filename
  12. path = validpath(filename);
  13.  
  14. % Cleanup tasks upon function completion
  15. cleanUp = onCleanup(@()xlsCleanup(Excel, path));
  16.  
  17. % Open Excel workbook.
  18. readOnly = true;
  19. [~, workbookHandle] = openExcelWorkbook (Excel, path, readOnly);
  20.  
  21. % Initialise worksheets object
  22. workSheets = workbookHandle.Worksheets;
  23.  
  24. % Get the sheet object (sheet #1)
  25. sheet = get(workSheets,'item',1);
  26.  
  27. % Print table headers
  28. fprintf('Color t Int t R G Bn');
  29. fprintf('--------t --------t -----n');
  30.  
  31. % Create figure
  32. figure;
  33. hold on;
  34.  
  35. % Loop through every color on the Excel file
  36. for row = 1:8
  37. % Get the cell object with name of color
  38. cell = get(sheet, 'Cells', row, 1);
  39. cName = cell.value;
  40.  
  41. % Get the cell object with colored background
  42. cell = get(sheet, 'Cells', row, 2);
  43.  
  44. % Get the interior object
  45. interior = cell.Interior;
  46.  
  47. % Get the color integer property
  48. cInt = get(interior, 'Color');
  49.  
  50. % Get the RGB triplet from its integer value
  51. cRGB = int2rgb(cInt);
  52.  
  53. % Plot the color
  54. patch([0 0 1 1], [8-row 9-row 9-row 8-row], cRGB);
  55.  
  56. % Print row with color data
  57. fprintf('%-8st %8dt %d %d %dn', cName, cInt, cRGB);
  58. end
  59.  
  60. % Turn off axes
  61. set(findobj(gcf, 'type','axes'), 'Visible','off')
  62.  
  63. end
  64.  
  65. function[RGB] = int2rgb(colorInt)
  66. % Returns RGB triplet of an RGB integer.
  67.  
  68. if colorInt > 16777215 || colorInt < 0
  69. error ('Invalid int value. Valid range: 0 <= value <= 16777215')
  70. end
  71. R = floor(colorInt / (256*256));
  72. G = floor((colorInt - R*256*256)/256);
  73. B = colorInt - R*256*256 - G*256;
  74.  
  75. RGB = [R, G, B]/255;
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement