13.7 Color Palettes. 4tH 4tH Forth compiler
Reklama
Reklama
CHAPTER 13. SPECIAL LIBRARIES
13.7
Color Palettes
10
210
Since the Netpbm grayscale images are 3 times smaller than the color ones, one way to improve the efficiency (if true color resolution is not needed) is to store the color information in the grayscale format. While the setup shown here is not that useful, it was easily implemented. Instead of converting a color pixel to a grayscale pixel, a 6 level rgb format is used. For this, colors have a maximum intensity of 5, and the conversion is : palette =
38*red + 6*green + blue giving a maximum value of 215. The word ”PALETTE_IMAGE” will set the correct defaults, e.g.: include lib/graphics.4th
include lib/gturtle.4th
include lib/palette.4th
300 pic_width !
300 pic_height !
palette_image
( use some graphical routines) s" ptest.ppm" save_image
This will save the image as a PGM image (with a P5 magic word). Thus when you view the image, it will appear as a strange looking grayscale image (as another example, try the gtest4.4th example program) To view ptest.ppm in color, you must convert the image to a PPM color file. There are two example programs to do this. One is color2pal.4th
and the other is pal2col.4th. Note for this particular color palette, once it is converted back to the color format (with a P6 magic word) it should have a maximum pixel intensity of 5. Unfortunately, many graphical viewers have trouble displaying this correctly (though many work fine). Therefore, the col2pal.4th example will scale up the pixels values to the 255 level.
Conversion between color and grayscale pixels can be performed using the words ”COLOR>GRAY” and ”GRAY>COLOR”. If color palette and/or grayscale images are defined, the words
”COLOR>N” and ”N>COLOR” can be used instead.
W
ORD color>gray gray>color to_color to_gray color_image grayscale_image palette_image color>palette
S
TACK
E
FFECT
C
OMMENT rgb – n n – rgb
–
–
–
–
– rgb – n
Covert 3 byte color format to 1 byte grayscale
Convert grayscale to color format.
The pixel still has the gray color though
Set the library primitives so that pixel and color data will use 3 bytes.
Set library primitives so that pixel and color data will use 1 byte
Set defaults for using color (PPM) images
Set defaults for using grayscale
(PGM) images
Set defaults for using color palette.
Similar to grayscale and image will be save in PGM format.
Convert color information to palette code
10
Article written and contributed by David Johnson.
Stiahnuť
Reklama