Color to Monochrome

Post Reply
User avatar
rbytes
Posts: 1798
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Color to Monochrome

Post by rbytes » Fri Mar 09, 2018 4:18 pm

Smart Basic can convert a color image to monochrome.

31B67BA6-3081-4797-9770-49D0A0B9608F.jpeg
31B67BA6-3081-4797-9770-49D0A0B9608F.jpeg (497.65 KiB) Viewed 1363 times

You may think of monochrome as black and white, and that is partly true. But a black and white image can also have a tint applied, and still fall into the monochrome category. The original color image and a monochrome image tinted cyan can be seen below.

This program runs on iPad only. The pixel count of smaller devices makes it impossible to process an image of sufficient size.

Code: Select all

/*
Color to Monochrome
By rbytes, March 2018

This app will allow you to select an image from a Smart Basic folder or the Camera Roll and convert it to monochrome. With the rshift(red), gshift(green) and bshift(blue) values set to 1, you will get a perfect black and white image. If you modify the shift values by a small percentage (eg. .9 or 1.1, you can produce a tinted monochrome.

How do you know that the proper shade of grey is produced for each colored area of the image? The eyes are most sensitive to green light. The brightness you perceive is contributed 59% from frequencies in the green range, 30% in the red range and 11% in the blue range. Check the conversion algorithm to see how I applied these percentages.
*/

'comment or uncomment the next 2 lines to determine if loading an image file
im$="head"  ' set the string to an image file in Smart Basic, minus extender
ext$=".jpg"  ' set the string to the image file's extender

' comment or uncomment the next 3 lines to determine if importing from the Camera Roll
'im$="temp"
'ext$=".jpg"
'ALBUM IMPORT im$&ext$

s=.38  ' scale to draw the image (experiment to get the size you want)
rshift=.8!gshift=1!bshift=1
SET TOOLBAR OFF
GET IMAGE im$&ext$ SIZE W,H
GET SCREEN SIZE sw,sh
GRAPHICS
BUTTON "stop" TEXT "STOP" AT sw-80,20
GRAPHICS CLEAR
DRAW IMAGE im$&ext$ AT 0,0 SCALE s
FOR y=0 TO H*2*s
  FOR x=0 TO W*2*s
    GET PIXEL x,y COLOR R,G,B,A
    ' the next line is the algorithm for luminance (brightness)
    r1=R*.3+G*.59+B*.11!g1=r1*gshift!b1=r1*bshift!r1=r1*rshift!a1=A
    DRAW PIXEL x,y COLOR r1,g1,b1,a1
  NEXT x
  IF BUTTON_PRESSED("stop") THEN END
NEXT y
GRAPHICS SAVE 0,0,W*s,H*s TO im$&"-mono"&ext$
ALBUM EXPORT im$&"-mono"&ext$
Attachments
C94D51F7-FBC4-4371-8111-D4B3099D23FB.jpeg
C94D51F7-FBC4-4371-8111-D4B3099D23FB.jpeg (3.62 MiB) Viewed 1362 times
93C78553-DDA6-4714-AF40-599766FACD0E.jpeg
93C78553-DDA6-4714-AF40-599766FACD0E.jpeg (513.38 KiB) Viewed 1363 times
####### Living the colorful life #######

Post Reply