hex2rgb.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. function [ rgb ] = hex2rgb(hex,range)
  2. % hex2rgb converts hex color values to rgb arrays on the range 0 to 1.
  3. %
  4. %
  5. % * * * * * * * * * * * * * * * * * * * *
  6. % SYNTAX:
  7. % rgb = hex2rgb(hex) returns rgb color values in an n x 3 array. Values are
  8. % scaled from 0 to 1 by default.
  9. %
  10. % rgb = hex2rgb(hex,256) returns RGB values scaled from 0 to 255.
  11. %
  12. %
  13. % * * * * * * * * * * * * * * * * * * * *
  14. % EXAMPLES:
  15. %
  16. % myrgbvalue = hex2rgb('#334D66')
  17. % = 0.2000 0.3020 0.4000
  18. %
  19. %
  20. % myrgbvalue = hex2rgb('334D66') % <-the # sign is optional
  21. % = 0.2000 0.3020 0.4000
  22. %
  23. %
  24. % myRGBvalue = hex2rgb('#334D66',256)
  25. % = 51 77 102
  26. %
  27. %
  28. % myhexvalues = ['#334D66';'#8099B3';'#CC9933';'#3333E6'];
  29. % myrgbvalues = hex2rgb(myhexvalues)
  30. % = 0.2000 0.3020 0.4000
  31. % 0.5020 0.6000 0.7020
  32. % 0.8000 0.6000 0.2000
  33. % 0.2000 0.2000 0.9020
  34. %
  35. %
  36. % myhexvalues = ['#334D66';'#8099B3';'#CC9933';'#3333E6'];
  37. % myRGBvalues = hex2rgb(myhexvalues,256)
  38. % = 51 77 102
  39. % 128 153 179
  40. % 204 153 51
  41. % 51 51 230
  42. %
  43. % HexValsAsACharacterArray = {'#334D66';'#8099B3';'#CC9933';'#3333E6'};
  44. % rgbvals = hex2rgb(HexValsAsACharacterArray)
  45. %
  46. % * * * * * * * * * * * * * * * * * * * *
  47. % Chad A. Greene, April 2014
  48. %
  49. % Updated August 2014: Functionality remains exactly the same, but it's a
  50. % little more efficient and more robust. Thanks to Stephen Cobeldick for
  51. % the improvement tips. In this update, the documentation now shows that
  52. % the range may be set to 256. This is more intuitive than the previous
  53. % style, which scaled values from 0 to 255 with range set to 255. Now you
  54. % can enter 256 or 255 for the range, and the answer will be the same--rgb
  55. % values scaled from 0 to 255. Function now also accepts character arrays
  56. % as input.
  57. %
  58. % * * * * * * * * * * * * * * * * * * * *
  59. % See also rgb2hex, dec2hex, hex2num, and ColorSpec.
  60. %
  61. %% Input checks:
  62. assert(nargin>0&nargin<3,'hex2rgb function must have one or two inputs.')
  63. if nargin==2
  64. assert(isscalar(range)==1,'Range must be a scalar, either "1" to scale from 0 to 1 or "256" to scale from 0 to 255.')
  65. end
  66. %% Tweak inputs if necessary:
  67. if iscell(hex)
  68. assert(isvector(hex)==1,'Unexpected dimensions of input hex values.')
  69. % In case cell array elements are separated by a comma instead of a
  70. % semicolon, reshape hex:
  71. if isrow(hex)
  72. hex = hex';
  73. end
  74. % If input is cell, convert to matrix:
  75. hex = cell2mat(hex);
  76. end
  77. if strcmpi(hex(1,1),'#')
  78. hex(:,1) = [];
  79. end
  80. if nargin == 1
  81. range = 1;
  82. end
  83. %% Convert from hex to rgb:
  84. switch range
  85. case 1
  86. rgb = reshape(sscanf(hex.','%2x'),3,[]).'/255;
  87. case {255,256}
  88. rgb = reshape(sscanf(hex.','%2x'),3,[]).';
  89. otherwise
  90. error('Range must be either "1" to scale from 0 to 1 or "256" to scale from 0 to 255.')
  91. end
  92. end