Internal: A function to convert an RGB triple into the hexidecimal equivalent
Anonym
Here's a function to convert an RGB triple into the hexidecimal equivalent:
Function GetHex, number
; This FUNCTION accepts a (red, green, blue) triple that
; describes a particular color and returns a 24-bit long
; integer that is equivalent to that color. The color is
; described in terms of a hexidecimal number (e.g., FF206A)
; where the left two digets represent the blue color, the
; middle two digets represent the green color, and the right
; two digets represent the red color.
; The variable number must be have at least one element
num_values = N_Elements(number)
IF num_values NE 3 THEN $
Message,'The argument to GetHex must be an array with 3 elements.'
hexnumbers = ['0', '1', '2', '3', '4', '5', '6', $
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
; Pull out the red, green, and blue colors to make a
; string variable representing a hexidecimal number.
hexnum = strarr(num_values)
arg = ''
for i = 0, num_values-1 DO BEGIN
sixteens = number(i)/16
ones = number(i) MOD 16
hexnum(i) = hexnumbers(sixteens) + hexnumbers(ones)
arg = StrCompress(hexnum(i),/Remove_All) + arg
ENDFOR
; Create the hexidecimal number. Make sure it is stored
; as a LONG integer.
check = Execute("hexnum = Long('" + arg + "'X)")
RETURN, hexnum
END ; *************************** of GetHex ********************************
Function WhatTypeVar, variable
; Use SIZE function to get variable info.
varInfo = Size(variable)
; How long is the returned vector?
lengthOfVariable = N_Elements(varInfo)
; The next to last element in varInfo has the data type.
typeIndex = varInfo(lengthOfVariable - 2)
dataTypes = ['UNDEFINED', 'BYTE', 'INTEGER', 'LONG', 'FLOATING', $
'DOUBLE', 'COMPLEX', 'STRING', 'STRUCTURE', 'DCOMPLEX']
thisType = dataTypes(typeIndex)
RETURN, thisType
END ; ********************** of WhatTypeVar *********************************
Function GetColor, color, All=all, Hexidecimal=hex
;+
; The GetColor function returns the (x,y,z) triple that describes
; a particular color. The following 12 colors are available:
;
; Color Name Red Green Blue
; ---------- --- ----- ----
; BLACK 0 0 0
; WHITE 255 255 255
; RED 255 0 0
; ORANGE 255 127 255
; YELLOW 255 255 0
; GREEN 0 255 0
; TURQUOISE 0 255 200
; LT BLUE 0 150 255
; BLUE 0 0 255
; PINK 255 168 182
; MAGENTA 78 0 182
; PURPLE 69 0 77
;
; The ALL keyword returns all 12 colors in a named (12,3) variable.
;
; The HEXIDECIMAL keyword returns a 24-bit LONG integer that
; is the 24-bit equivalent of the (r,g,b) triple. If the ALL
; keyword is used in conjuction with this keyword, the ALL
; keyword returns a 12 element LONG vector.
;
; USAGE:
;
; To load a yellow color at color index 12:
;
; yellow = GetColor('yellow')
; TVLct, yellow, 12
;
; To load all 12 colors starting a color index 120:
;
; dummy = GetColor(All=drawColors)
; TVLct, drawColors, 120
;
; To draw in a yellow color on a 24-bit system:
;
; yellow = GetColor('yellow', /Hexidecimal)
; Plot, Findgen(11), Color=yellow
;-
; Error handling. Return to main-level.
On_Error, 1
; Set up colors and indices.
drawColors = ['BLACK', 'WHITE', 'RED', 'ORANGE', 'YELLOW', 'GREEN',$
'TURQUOISE', 'LT BLUE', 'BLUE', 'PINK', 'MAGENTA', 'PURPLE']
red = [0B, 255B, 255B, 255B, 255B, 0B, 0B, 0B, 0B, 255B, 78B, 69B ]
green = [0B, 255B, 0B, 127B, 255B, 255B, 255B, 150B, 0B, 168B, 0B, 0B ]
blue = [0B, 255B, 0B, 0B, 0B, 0B, 200B, 255B, 255B, 182B, 182B, 77B ]
; If no color parameter is specified, return a green color, otherwise check the
; color requested and return a green if it's not valid.
IF N_Params() EQ 0 THEN colorIndex = 5 $
ELSE BEGIN
; If the parameter is not a string variable this is an error.
paramType = WhatTypeVar(color)
IF paramType NE 'STRING' THEN Message,'Augument must be STRING type.'
; Get color name in UPPERCASE characters.
strColor = StrUpCase(color)
; Can you find this name in drawColors array? If not, use GREEN.
colorIndex = Where(drawColors EQ strColor, count)
IF count EQ 0 THEN BEGIN
messageText = "Can't find color " + '"' + color + '"' + " in color list."
ok = Widget_Message([messageText,"Returning a GREEN color."])
colorIndex = 5
ENDIF
ENDELSE
; The user may want all 12 colors. Indicated by using the ALL keyword.
IF Keyword_Set(hex) THEN BEGIN
all = LonArr(12)
FOR j=0,11 DO all(j) = GetHex(Reform([red(j), green(j), blue(j)], 1, 3))
ENDIF ELSE BEGIN
all = BytArr(12,3)
all(*,0) = red
all(*,1) = green
all(*,2) = blue
ENDELSE
; OK, return the color triple corresponding to this color.
; Return a [1,3] array so we can send it directly to TvLct.
returnColor = Reform([red(colorIndex), green(colorIndex), blue(colorIndex)], 1, 3)
; Get a hexidecimal value if one is required.
IF Keyword_Set(hex) THEN returnColor = GetHex(returnColor)
RETURN, returnColor
END