use M_color, only : & & hue, & & closest_color_name, & & color_name2rgb, & & rgbmono
Highly accurate color conversions are a tricky business, and color is a complex topic; but these simplified conversions between common color models work quite well for basic conversions.
Typically the only user routine called is HUE(3f). HUE(3f) is a single routine that interfaces to all the private low-level color conversion routines to convert a color's components from one color model to another. HUE(3f) converts between the following color models:
In addition to the reversible color model conversions there are a few other user-callable color-related procedures:
CLOSEST_COLOR_NAME: given RGB values, try to find closest named colorThe rest of the library is composed of PRIVATE procedures. For each color model supported the general idea of the module is that there are two routines for each color model:
This allows conversions between all color models with only 2*N routines. That is, to go from model A to model B the module would internally make two calls:
call modelA2rgb(...) call rgb2modelB(...)
The resulting internal routines that result are:
Sample program:
A simple interactive javascript-based color selector lets you interactively select colors.
The color wheel below was generated using a M_draw(3f) graphics library program (huegif.f90) that uses the M_color module.
subroutine rgbhls(r,g,b,h,l,s,status) ! red component as a value of 0 to 100 real, intent(in) :: r ! green component as a value of 0 to 100 real, intent(in) :: g ! blue component as a value of 0 to 100 real, intent(in) :: b ! hue value in the range of 0 to 360 degrees real, intent(out) :: h ! lightness as a percent value from 0 to 100 real, intent(out) :: l ! saturation as a percent from 0 to 100 real, intent(out) :: s integer :: status
Color RGB HLS Sample Red 100.0 0.0 0.0 0 50.0 100.0 Yellow 100.0 100.0 0.0 60 50.0 100.0 Green 0.0 100.0 0.0 120 50.0 100.0 Cyan 0.0 100.0 100.0 180 50.0 100.0 Blue 0.0 0.0 100.0 240 50.0 100.0 Magenta 100.0 0.0 100.0 300 50.0 100.0 White 100.0 100.0 100.0 (any) 100.0 (any) Black 0.0 0.0 0.0 (any) 0.0 (any) Maroon 50.0 0.0 0.0 0 25.0 100.0 Pink 100.0 50.0 50.0 0 75.0 100.0
subroutine rgbhvs(r,g,b,h,v,s,status) ! the red component as a value of 0 to 100. real, intent(in) :: r ! the green component as a value of 0 to 100. real, intent(in) :: g ! the blue component as a value of 0 to 100. real, intent(in) :: b ! the hue value in the range of 0 to 360 degrees real, intent(out) :: h ! the "value" as a percent value from 0 to 100. real, intent(out) :: v ! the saturation as a percent from 0 to 100. real, intent(out) :: s integer :: status
Color Color
nameHex (R,G,B) (H,S,V) Black #000000 (0,0,0) (0º,0%,0%) White #FFFFFF (100,100,100) (0º,0%,100%) Red #FF0000 (100,0,0) (0º,100%,100%) Lime #00FF00 (0,100,0) (120º,100%,100%) Blue #0000FF (0,0,100) (240º,100%,100%) Yellow #FFFF00 (100,100,0) (60º,100%,100%) Cyan #00FFFF (0,100,100) (180º,100%,100%) Magenta #FF00FF (100,0,100) (300º,100%,100%) Silver #C0C0C0 (75,75,75) (0º,0%,75%) Gray #808080 (50,50,50) (0º,0%,50%) Maroon #800000 (50,0,0) (0º,100%,50%) Olive #808000 (50,50,0) (60º,100%,50%) Green #008000 (0,50,0) (120º,100%,50%) Purple #800080 (50,0,50) (300º,100%,50%) Teal #008080 (0,50,50) (180º,100%,50%) Navy #000080 (0,0,50) (240º,100%,50%)
subroutine cmyrgb(c,m,y,r,g,b,status) ! cyan component as a value in the range of 0 to 100 real, intent(in) :: c ! magenta component as a value in the range of 0 to 100 real, intent(in) :: m ! yellow component as a value in the range of 0 to 100 real, intent(in) :: y ! red component as a value in the range of 0 to 100 real, intent(out) :: r ! green component as a value in the range of 0 to 100 real, intent(out) :: g ! blue component as a value in the range of 0 to 100 real, intent(out) :: b integer :: status
subroutine rgbcmy(r,g,b,c,m,y,status) ! the red component as a value in the range of 0 to 100 real, intent(in) :: r ! the green component as a value in the range of 0 to 100 real, intent(in) :: g ! the blue component as a value in the range of 0 to 100 real, intent(in) :: b ! the cyan component as a value in the range of 0 to 100 real, intent(out) :: c ! the magenta component as a value in the range of 0 to 100 real, intent(out) :: m ! the yellow component as a value in the range of 0 to 100 real, intent(out) :: y integer :: status
Color Color name
(C,M,Y) ( R, G, B) Hex Black (100,100,100) ( 0, 0, 0) #000000 White ( 0, 0, 0) (100,100,100) #FFFFFF Red ( 0,100,100) (100, 0, 0) #FF0000 Green (100, 0,100) ( 0,100, 0) #00FF00 Blue (100,100, 0) ( 0, 0,100) #0000FF Yellow ( 0, 0,100) (100,100, 0) #FFFF00 Cyan (100, 0, 0) ( 0,100,100) #00FFFF Magenta ( 0,100, 0) (100, 0,100) #FF00FF
subroutine rgbval(clr1,clr2,h) integer, intent(in) :: h ! H is the hue value in degrees real, intent(in) :: clr1 ! real, intent(in) :: clr2 !
subroutine hlsrgb (h,l,s,r,g,b,status) ! hue value in the range of 0 to 360 degrees real, intent(in) :: h ! lightness as a percent value from 0 to 100. real, intent(in) :: l ! saturation as a percent from 0 to 100. real, intent(in) :: s ! red component as a value of 0 to 100. real, intent(out) :: r ! green component as a value of 0 to 100. real, intent(out) :: g ! blue component as a value of 0 to 100. real, intent(out) :: b integer :: status
subroutine hvsrgb(h,v,s,r,g,b,status) ! H is the hue value in the range of 0 to 360 degrees real, intent(in) :: h ! V is the "value" as a percent value from 0 to 100. real, intent(in) :: v ! S is the saturation as a percent from 0 to 100. real, intent(in) :: s ! R is the red component as a value of 0 to 100. real, intent(out) :: r ! G is the green component as a value of 0 to 100. real, intent(out) :: g ! B is the blue component as a value of 0 to 100. real, intent(out) :: b integer :: status
subroutine yiqrgb(y,i,q,r,g,b,status) real,intent(in) :: y,i,q real,intent(out) :: r,g,b integer :: status
subroutine rgbyiq(r,g,b,y,i,q,status) real,intent(in) :: r,g,b real,intent(out) :: y,i,q integer :: status