snibgo's ImageMagick pages

Colour cycling

Cycling colours with different methods gives results that satisfy certain criteria.

For cyclic animations of images of fractal noise, or images that have been distorted by fractal noise (see Fractal noise animations), I needed an ImageMagick process that would take a noise image and a percentage. At slightly different percentages it would create slightly different noise images, but 100% would give the same result as 0% (making it cyclic).

The problem

For convenience, this page considers just one channel, so the examples are grayscale. The same methods can be applied to three channels independently.

We have a grayscale image with values that are distributed evenly from black to white, and the mean is 50%. All values are equally likely. We want to add a value X% (0 <= X <= 100) to every pixel, to create a new image. How can we do this? There is an obvious method: "-evaluate AddModulus" does the job. This is quick and easy. If we add a small value, such as 1%, most pixels will change a small amount. Unfortunately, some pixels will change a large amount; pixels that were slightly beneath white will become slightly above black. This may be undesirable.

So we would like a number of criteria to be satisfied.

Other criteria may be desirable, for example that the standard deviation should be constant.

I don't think any method satisfies all criteria. In particular, I don't think an animated technique can have no sudden changes while also being reversible.

(For some purposes, strict reversibility is not important. Provided we can find some input that gives the desired output, we don't care if other inputs can also give that same output.)

The methods

Create a simple image that will be animated by colour-cycling, using each method.

del ccy_*.miff

set SRC=ccy_src.png

%IM%convert ^
  -seed 1234 ^
  -size 200x200 ^
  ( xc: +noise Random ^
    -channel R -separate +channel ) ^
  gradient: ^
  -append ^
  %SRC%
ccy_src.png

For each method, we can create an animation. A loop is run with %%i as the loop counter. Environment variable LZ is set to this value with six digits and leading zeros. (I always use six digits for frame filenames, although six is overkill here.) convert creates each frame file. When the loop is finished, another convert reads all the frame files and creates an animated GIF, which is shown.

The top half of the animation shows what the method does to a noise image. The bottom half shows what the method does to a gradient image, which helps to explain the process.

Each method also calls the script ccy_stats.bat, which calculates some statistics for that method.

The statistics could be collected for every pair of adjacent frames.

Commands here use Windows cmd arithmetic, which assumes all numbers are integers. For real use, we should use floating-point arithmetic.

We will generate frames from 0 to 99, but skip even-numbered frames to save time and space.

set FOR_ARGS=0,2,99
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  %IM%convert ^
    %SRC% ^
    -evaluate AddModulus %%i%% ^
    ccy_am_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_am_*.miff ^
  ccy_am.gif

call %PICTBAT%ccy_stats am
zeroAE=0  zeroRMSE=0 (0)  ch20=80000
pae1820=64225 (0.980011)  mae1820=2539.4 (0.0387487)
mean20=0.49948
ccy_am.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A PC=2*%%i

  %IM%convert ^
    %SRC% ^
    -evaluate AddModulus !PC!%% ^
    ccy_am2_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_am2_*.miff ^
  ccy_am2.gif

call %PICTBAT%ccy_stats am2
zeroAE=0  zeroRMSE=0 (0)  ch20=80000
pae1820=62915 (0.960021)  mae1820=4999.6 (0.076289)
mean20=0.500252
ccy_am2.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  %IM%convert ^
    %SRC% ^
    -evaluate AddModulus %%i%% ^
    -solarize 50%% ^
    -evaluate Multiply 2 ^
    ccy_amsol_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_amsol_*.miff ^
  ccy_amsol.gif

call %PICTBAT%ccy_stats amsol
zeroAE=79799  zeroRMSE=26814.1 (0.409157)  ch20=79998
pae1820=2622 (0.0400092)  mae1820=2570.65 (0.0392255)
mean20=0.499652
ccy_amsol.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A PC=%%i
  set /A NEGPC=100-!PC!

  %IM%convert ^
    %SRC% ^
    ^( -clone 0 -evaluate AddModulus !PC!%% ^) ^
    ^( -clone 1 -negate ^) ^
    ^( -clone 0 -threshold !NEGPC!%% ^) ^
    -delete 0 ^
    -composite ^
    ccy_amneg_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_amneg_*.miff ^
  ccy_amneg.gif

call %PICTBAT%ccy_stats amneg
zeroAE=0  zeroRMSE=0 (0)  ch20=79998
pae1820=1311 (0.0200046)  mae1820=1298.36 (0.0198118)
mean20=0.659661
ccy_amneg.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A PC=%%i
  set /A NEGPC=100-!PC!

  %IM%convert ^
    %SRC% ^
    ^( -clone 0 -evaluate AddModulus !PC!%% ^) ^
    ^( -clone 1 -negate ^) ^
    ^( -clone 0 -threshold !NEGPC!%% ^) ^
    -delete 0 ^
    -composite ^
    -auto-level ^
    -auto-gamma ^
    ccy_amnegaa_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_amnegaa_*.miff ^
  ccy_amnegaa.gif

call %PICTBAT%ccy_stats amnegaa
zeroAE=0  zeroRMSE=0 (0)  ch20=79798
pae1820=2555 (0.0389868)  mae1820=961.498 (0.0146715)
mean20=0.524504
ccy_amnegaa.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A PC=2*%%i
  set /A NEGPC=100-!PC!

  if /I !NEGPC! LEQ 0 (
    set /A NEGPC=-!NEGPC!
    set /A PC=200-!PC!
  )

  %IM%convert ^
    %SRC% ^
    ^( -clone 0 -evaluate AddModulus !PC!%% ^) ^
    ^( -clone 1 -negate ^) ^
    ^( -clone 0 -threshold !NEGPC!%% ^) ^
    -delete 0 ^
    -composite ^
    ccy_amneg2_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_amneg2_*.miff ^
  ccy_amneg2.gif

call %PICTBAT%ccy_stats amneg2
zeroAE=0  zeroRMSE=0 (0)  ch20=80000
pae1820=2621 (0.0399939)  mae1820=2569.98 (0.0392153)
mean20=0.739292
ccy_amneg2.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A PC=2*%%i
  set /A NEGPC=100-!PC!

  if /I !NEGPC! LEQ 0 (
    set /A NEGPC=-!NEGPC!
    set /A PC=200-!PC!
  )

  %IM%convert ^
    %SRC% ^
    ^( -clone 0 -evaluate AddModulus !PC!%% ^) ^
    ^( -clone 1 -negate ^) ^
    ^( -clone 0 -threshold !NEGPC!%% ^) ^
    -delete 0 ^
    -composite ^
    -auto-level ^
    -auto-gamma ^
    ccy_amneg2aa_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_amneg2aa_*.miff ^
  ccy_amneg2aa.gif

call %PICTBAT%ccy_stats amneg2aa
zeroAE=0  zeroRMSE=0 (0)  ch20=79799
pae1820=5831 (0.0889754)  mae1820=3839.58 (0.0585882)
mean20=0.516628
ccy_amneg2aa.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A PC=2*%%i
  set /A NEGPC=100-!PC!

  if /I !NEGPC! LEQ 0 (
    set /A NEGPC=!NEGPC!+100
    set sNEG=-negate
  ) else (
    set sNEG=
  )

  %IM%convert ^
    %SRC% ^
    ^( -clone 0 -evaluate AddModulus !PC!%% ^
       !sNEG! ^) ^
    ^( -clone 1 -negate ^) ^
    ^( -clone 0 -threshold !NEGPC!%% ^) ^
    -delete 0 ^
    -composite ^
    ccy_amneg3_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_amneg3_*.miff ^
  ccy_amneg3.gif

call %PICTBAT%ccy_stats amneg3
zeroAE=0  zeroRMSE=0 (0)  ch20=80000
pae1820=2621 (0.0399939)  mae1820=2569.98 (0.0392153)
mean20=0.739292
ccy_amneg3.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A PC=200-2*%%i
  set /A NEGPC=100-!PC!

  if /I !NEGPC! LSS 0 (
    set /A NEGPC=!NEGPC!+100
    set sNEG=-negate
  ) else (
    set sNEG=
  )

  %IM%convert ^
    %SRC% ^
    ^( -clone 0 -evaluate AddModulus !PC!%% ^
       !sNEG! ^) ^
    ^( -clone 1 -negate ^) ^
    ^( -clone 0 -threshold !NEGPC!%% ^) ^
    -delete 0 ^
    -composite ^
    ccy_amneg3n_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_amneg3n_*.miff ^
  ccy_amneg3n.gif

call %PICTBAT%ccy_stats amneg3n
zeroAE=200  zeroRMSE=3276.75 (0.05)  ch20=80000
pae1820=2621 (0.0399939)  mae1820=2569.21 (0.0392036)
mean20=0.260682
ccy_amneg3n.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A PC=2*%%i
  set /A NEGPC=100-!PC!

  if /I !NEGPC! LEQ 0 (
    set /A NEGPC=!NEGPC!+100
    set sNEG=-negate
  ) else (
    set sNEG=
  )

  %IM%convert ^
    %SRC% ^
    ^( -clone 0 -evaluate AddModulus !PC!%% ^
       !sNEG! ^) ^
    ^( -clone 1 -negate ^) ^
    ^( -clone 0 -threshold !NEGPC!%% ^) ^
    -delete 0 ^
    -composite ^
    -auto-level ^
    -auto-gamma ^
    ccy_amneg3aa_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_amneg3aa_*.miff ^
  ccy_amneg3aa.gif

call %PICTBAT%ccy_stats amneg3aa
zeroAE=0  zeroRMSE=0 (0)  ch20=79799
pae1820=5831 (0.0889754)  mae1820=3839.58 (0.0585882)
mean20=0.516628
ccy_amneg3aa.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A DEG=%%i*36/10-90

  %IM%convert ^
    %SRC% ^
    -function sinusoid 1,!DEG!,0.5,0.5 ^
    ccy_sin_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_sin_*.miff ^
  ccy_sin.gif

call %PICTBAT%ccy_stats sin
zeroAE=79800  zeroRMSE=29961.7 (0.457187)  ch20=80000
pae1820=4572 (0.0697642)  mae1820=2909.98 (0.0444034)
mean20=0.499434
ccy_sin.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A DEG=%%i*36/10-90

  %IM%convert ^
    %SRC% ^
    -function sinusoid 0.5,!DEG!,0.5,0.5 ^
    ccy_sin2_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_sin2_*.miff ^
  ccy_sin2.gif

call %PICTBAT%ccy_stats sin2
zeroAE=79599  zeroRMSE=4940.92 (0.0753936)  ch20=80000
pae1820=4572 (0.0697642)  mae1820=2915.3 (0.0444847)
mean20=0.801952
ccy_sin2.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A DEG=%%i*36/10-90

  %IM%convert ^
    %SRC% ^
    -function sinusoid 0.5,!DEG!,0.5,0.5 ^
    -auto-level ^
    -auto-gamma ^
    ccy_sin2aa_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_sin2aa_*.miff ^
  ccy_sin2aa.gif

call %PICTBAT%ccy_stats sin2aa
zeroAE=79598  zeroRMSE=4941.33 (0.0753999)  ch20=79767
pae1820=9745 (0.148699)  mae1820=4437.41 (0.0677105)
mean20=0.572419
ccy_sin2aa.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A DEG=%%i*36/10-90

  %IM%convert ^
    %SRC% ^
    -function sinusoid 1,!DEG!,0.5,0.5 ^
    -function arcsin 1 ^
    ccy_sinarc_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_sinarc_*.miff ^
  ccy_sinarc.gif

call %PICTBAT%ccy_stats sinarc
zeroAE=79799  zeroRMSE=26814.3 (0.40916)  ch20=80000
pae1820=3025 (0.0461585)  mae1820=2849.59 (0.043482)
mean20=0.49966
ccy_sinarc.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A DEG=%%i*36/10-90

  %IM%convert ^
    %SRC% ^
    -function sinusoid 0.5,!DEG!,0.5,0.5 ^
    -function arcsin 1 ^
    ccy_sinarc2_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_sinarc2_*.miff ^
  ccy_sinarc2.gif

call %PICTBAT%ccy_stats sinarc2
zeroAE=16028  zeroRMSE=3.18962 (4.86704e-005)  ch20=80000
pae1820=3025 (0.0461585)  mae1820=2849.71 (0.0434838)
mean20=0.739287
ccy_sinarc2.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A DEG=270-%%i*36/10

  %IM%convert ^
    %SRC% ^
    -function sinusoid 0.5,!DEG!,0.5,0.5 ^
    -function arcsin 1 ^
    ccy_sinarc2n_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_sinarc2n_*.miff ^
  ccy_sinarc2n.gif

call %PICTBAT%ccy_stats sinarc2n
zeroAE=16028  zeroRMSE=3.18962 (4.86704e-005)  ch20=80000
pae1820=3025 (0.0461585)  mae1820=2849.28 (0.0434773)
mean20=0.260687
ccy_sinarc2n.gif

Combine sinarc2 and sinarc2n,
masked over using least-significant bit.

for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A DEG1=%%i*36/10-90
  set /A DEG2=270-%%i*36/10

  %IM%convert ^
    %SRC% ^
    ^( -clone 0 ^
       -function sinusoid 0.5,!DEG1!,0.5,0.5 ^
       -function arcsin 1 ^
    ^) ^
    ^( -clone 0 ^
       -function sinusoid 0.5,!DEG2!,0.5,0.5 ^
       -function arcsin 1 ^
    ^) ^
    ^( -clone 0 ^
       -evaluate And 1 -fill White +opaque Black ^
    ^) ^
    -delete 0 ^
    -composite ^
    ccy_sinarc2o_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_sinarc2o_*.miff ^
  ccy_sinarc2o.gif

call %PICTBAT%ccy_stats sinarc2o
zeroAE=16028  zeroRMSE=3.18962 (4.86704e-005)  ch20=80000
pae1820=3025 (0.0461585)  mae1820=2858.43 (0.0436169)
mean20=0.499452

The changes in the noisy half are smooth,
with no overall change in brightness.

The gradient part has only 200 values,
so the least-significant bit has a beat frequency.

ccy_sinarc2o.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  set /A DEG=%%i*36/10-90

  %IM%convert ^
    %SRC% ^
    -function sinusoid 0.5,!DEG!,0.5,0.5 ^
    -function arcsin 1 ^
    -auto-level ^
    -auto-gamma ^
    ccy_sinarc2aa_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_sinarc2aa_*.miff ^
  ccy_sinarc2aa.gif

call %PICTBAT%ccy_stats sinarc2aa
zeroAE=16028  zeroRMSE=3.18962 (4.86704e-005)  ch20=79794
pae1820=6479 (0.0988632)  mae1820=4231.11 (0.0645626)
mean20=0.516626
ccy_sinarc2aa.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  if %%i LSS 50 (
    set /A PC=2*%%i
  ) else (
    set /A PC=200-2*%%i
  )
  set /A PC2=100-!PC!

  %IM%convert ^
    %SRC% ^
    +level !PC!%%,!PC2!%% ^
    ccy_lev1_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_lev1_*.miff ^
  ccy_lev1.gif

call %PICTBAT%ccy_stats lev1
zeroAE=0  zeroRMSE=0 (0)  ch20=79999
pae1820=2622 (0.0400092)  mae1820=1314.12 (0.0200521)
mean20=0.499998
ccy_lev1.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  if %%i LSS 50 (
    set /A PCL=%%i
    set /A PCH=%%i*3
    set /A PCH2=100-%%i
  ) else (
    set /A PCL=100-%%i
    set /A PCH=300-%%i*3
    set /A PCH2=%%i
  )
  set /A PCL2=100-!PCL!*3

  %IM%convert ^
    %SRC% ^
    ^( -clone 0 ^
       +level !PCL!%%,!PCL2!%% ^
    ^) ^
    ^( -clone 0 ^
       +level !PCH!%%,!PCH2!%% ^
    ^) ^
    ^( -clone 0 ^
       -threshold 50%% ^
    ^) ^
    -delete 0 ^
    -composite ^
    ccy_lev2_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_lev2_*.miff ^
  ccy_lev2.gif

call %PICTBAT%ccy_stats lev2
zeroAE=0  zeroRMSE=0 (0)  ch20=79999
pae1820=1311 (0.0200046)  mae1820=656.565 (0.0100185)
mean20=0.500095
ccy_lev2.gif
for /L %%i in (%FOR_ARGS%) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  if %%i LSS 50 (
    set /A PCL=%%i*2
    set /A PCL2=100-%%i*4
    set /A PCH=%%i*4
    set /A PCH2=100-%%i*2
  ) else (
    set /A PCL=200-%%i*2
    set /A PCL2=%%i*4-300
    set /A PCH=400-%%i*4
    set /A PCH2=%%i*2-100
  )

  %IM%convert ^
    %SRC% ^
    ^( -clone 0 ^
       +level !PCL!%%,!PCL2!%% ^
    ^) ^
    ^( -clone 0 ^
       +level !PCH!%%,!PCH2!%% ^
    ^) ^
    ^( -clone 0 ^
       -threshold 50%% ^
    ^) ^
    -delete 0 ^
    -composite ^
    -auto-level ^
    ccy_lev3_!LZ!.miff
)
%IM%convert ^
  -loop 0 ^
  ccy_lev3_*.miff ^
  ccy_lev3.gif

call %PICTBAT%ccy_stats lev3
zeroAE=0  zeroRMSE=0 (0)  ch20=79997
pae1820=9105 (0.138933)  mae1820=4562.4 (0.0696178)
mean20=0.500262
ccy_lev3.gif

Some animations have a single frame with black in the bottom row when it should be white. This arises because "-threshold N%" turns only values above N% to white, so "-threshold 0%" turns everything white, except black which remains black. A fix is to subtract a small value from N. This is messy is a BAT script, so isn't fixed the simple demonstrations above. However, it is fixed in colCyclStr.bat.

Results

How well do the methods satisfy the criteria?

The null method is not shown above, but is provided in the script colCyclStr.bat; it returns an empty string so there is no colour change.

Method Criteria
No change
at zero
Non-zero
changes all
No sudden
changes
Pixels change
equally
Mean 50%
stays 50%
Reversible
null pass fail pass (no changes at all) pass
am pass pass fail pass pass pass
am2 pass pass fail pass pass pass
amsol fail pass pass pass pass fail
amneg pass pass fail pass fail fail
amnegaa pass pass fail pass pass fail
amneg2 pass pass pass pass fail fail
amneg2aa pass pass pass pass pass fail
amneg3 pass pass pass pass fail fail
amneg3n pass pass pass pass fail fail
amneg3aa pass pass pass pass pass fail
sin fail pass pass fail pass fail
sin2 fail pass pass pass fail fail
sin2aa fail pass pass pass slight fail fail
sinarc fail pass pass pass pass fail
sinarc2 pass pass pass pass fail fail
sinarc2n pass pass pass pass fail fail
sinarc2o pass pass pass pass pass fail
sinarc2aa pass pass pass pass pass fail
lev1 pass pass pass fail pass pass
lev2 pass pass pass fail pass pass
lev2 pass pass pass fail pass pass

Discussion

Where a method fails the "mean 50% stays 50%" test and the image cycles through dark and light, I provide an "aa" version of the same method. This adds "-auto-level -auto-gamma" to the method, which tends to keep the mean at 50% but sometimes gives a visual appearance of accelerating and decelerating the change of colour.

The "n" suffix denotes a reversal of the method without the "n". I have provided just two of these: amneg3n and sinarc2n.

Each method has strengths and weaknesses. Leaving aside the "Reversible" criteria (because I don't think it is important), methods amneg3aa, sinarc2aa and sinarc2o pass all the tests. However, the "aa" versions suffer the acceleration/deceleration appearance, and sinarc2o relies on the least significant bit being noisy. When the LSB is not noisy, the result has artefacts.

Method sinarc2o uses the least significant bit of the input image as a mask to choose one of two transformations (sinarc2 and sinarc2n) that both have "no change at zero" but have opposite mean-shifts. The resulting mean is kept constant, without the problems introduced by -auto-level -auto-gamma.

amsol and sinarc are both useful but fail the "no change at zero" test.

The methods that have "sudden changes" may have little practical use.

The goal is for uniformally distributed noise to be handled well, and to remain uniform. IM's +noise Random creates such noise. Other noise types are not uniform and cycling changes the distribution, causing visible pulsing at each cycle. See script nseCycl.bat (results not shown here). For non-uniform noise, method sinarc2o may be the most acceptable, but a better solution is to use +noise Random, apply a chosen method, then apply a clut that changes the distribution from uniform to Gaussian or whatever is desired (perhaps using the method shown in Process modules: matching histograms).

Practical use

If one of the methods is to be used, the appropriate code can be copied. If a choice of methods is to be provided, the script colCyclStr.bat can be called with a method name and percentage (which need not be an integer). It returns appropriate IM code in the variable ccsImStr, and this value can be used within a convert command.

For example:

call %PICTBAT%colCyclStr amneg

echo %ccsImStr% 
( -clone 0 -evaluate AddModulus %% )   ( -clone 1 -negate )   ( -clone 0 -threshold 100%% )   -delete 0 -compose Over -composite 

To get a list of method names available from colCyclStr, call the script colCyclList.bat:

call %PICTBAT%colCyclList 
null
am
am2
amsol
amneg
amnegaa
amneg2
amneg2aa
amneg3
amneg3n
amneg3aa
sin
sin2
sin2aa
sinarc
sinarc2
sinarc2n
sinarc2o
sinarc2aa
lev1
lev2
lev3

Colour cycle tests

The script colCyclTst.bat uses the above scripts to make a GIF animation from each method of a given input image. The input image can be colour or grayscale. To keep the GIFs small, I use the built-in "rose:".

call %PICTBAT%colCyclTst rose:
call procH1 cct_gifs

The script also builds HTML code for a web page that shows each GIF: Colour cycle tests.

Grayscale versus percent display

Each animation above shows a 2D (two-dimensional) image, transformed by a method with a different percentage to each frame. Instead, we can make a 1D image, transform it into other 1D images, and concatenate (append) them in the other dimension.

Make a 1D gradient.

set SRC1D=ccy_1d_src.png

%IM%convert ^
  -size 1x256 gradient: ^
  %SRC1D%
ccy_1d_src.png

Show the sinarc2aa method.

echo off
for /L %%i in (0,1,255) do (

  set LZ=000000%%i
  set LZ=!LZ:~-6!

  for /F "usebackq" %%L in (`%IM%identify ^
    -format "PC=%%[fx:100*%%i/256]" ^
    xc:`) do set %%L

  call %PICTBAT%colCyclStr sinarc2aa !PC!

  %IM%convert ^
    %SRC1D% ^
    !ccsImStr! ^
    ccy_1dsa_!LZ!.miff
)
echo on

%IM%convert ^
  ccy_1dsa_*.miff ^
  +append ^
  -pointsize 30 -gravity South ^
  label:sinarc2aa ^
  -append ^
  ccy_1dsa.png
ccy_1dsa.png

In the output image, the y-axis represents the input lightness and the x-axis represents the percentage. Each row shows the changes a pixel will undergo; for example, a pixel that is initially 25% will follow the changes shown in the row that is 25% of the way up an image. Each column shows the output values a grayscale gradient has for a given percentage.

We can put this is a loop, to make an output for each method.

set HTM=ccy_1dsa.htm
del %HTM%

call %PICTBAT%colCyclList >ccyList.lis

echo off

for /F %%M in (ccyList.lis) do (
  set METH=%%M
  echo method=!METH!

  for /L %%i in (0,1,255) do (

    set LZ=000000%%i
    set LZ=!LZ:~-6!

    for /F "usebackq" %%L in (`%IM%identify ^
      -format "PC=%%[fx:100*%%i/256]" ^
      xc:`) do set %%L

    call %PICTBAT%colCyclStr !METH! !PC!

    %IM%convert ^
      %SRC1D% ^
      !ccsImStr! ^
      ccy_1dsa_!LZ!.miff
  )

  %IM%convert ^
    ccy_1dsa_*.miff ^
    +append ^
    -pointsize 30 -gravity South ^
    label:!METH! ^
    -append ^
    ccy_1dsa_!METH!.png

  echo ^<img src="ccy_1dsa_!METH!.png" /^> >>%HTM%
)
echo on
ccy_1dsa_null.png ccy_1dsa_am.png ccy_1dsa_am2.png ccy_1dsa_amsol.png ccy_1dsa_amneg.png ccy_1dsa_amnegaa.png ccy_1dsa_amneg2.png ccy_1dsa_amneg2aa.png ccy_1dsa_amneg3.png ccy_1dsa_amneg3n.png ccy_1dsa_amneg3aa.png ccy_1dsa_sin.png ccy_1dsa_sin2.png ccy_1dsa_sin2aa.png ccy_1dsa_sinarc.png ccy_1dsa_sinarc2.png ccy_1dsa_sinarc2n.png ccy_1dsa_sinarc2o.png ccy_1dsa_sinarc2aa.png ccy_1dsa_lev1.png ccy_1dsa_lev2.png ccy_1dsa_lev3.png

Each result, except amneg and amnegaa, can tile horizontally.

Concatenating methods

The output of one method can be used as the input to the same or a different method. Some methods double frequencies, in that a black-white gradient is converted to a double black-white-black gradient. When two doublers are concatenated, we get a quadrupling of frequencies: black-white-black-white-black.

With concatenation, the two (or more) calls to colCyclStr.bat may have different percentages, perhaps changing at different speeds.

Future

A method could use cluts. For 0 to 50%, interpolate between identity clut (which is a gradient) and given clut. Then interpolate back to identity. We could have two cluts: interpolate from identity to first, then first to second, finally second to identity. Probably wise if caller also supplies the zero-frame clut.

The mask technique used in sinarc2o could be useful with other methods. Instead of the least significant bit, a different bit (or perhaps bit parity, or a noise image) might be used.

What method is like amneg2 but instead of the light band moving down then up, the dark band moves up then down?

A method could use a rolling clut, exactly the same size as there are frames in a cycle. Or, instead of rolling, use SRT with "-virtual-pixel tile".

Cleanup

:end

rem del ccy_*.miff

Scripts

For convenience, .bat scripts are also available in a single zip file. See Zipped BAT files.

ccy_stats.bat

set METH=%1

set LIS=ccy_stats_%METH%.lis

%IM%compare -metric AE -format "zeroAE=" ccy_%METH%_000000.miff %SRC% NULL: >%LIS% 2>&1
%IM%compare -metric RMSE -format "  zeroRMSE=" ccy_%METH%_000000.miff %SRC% NULL: >>%LIS% 2>&1
%IM%compare -metric AE -format "  ch20=" ccy_%METH%_000000.miff ccy_%METH%_000020.miff  NULL: >>%LIS% 2>&1
%IM%compare -metric PAE -format "\npae1820=" ccy_%METH%_000018.miff ccy_%METH%_000020.miff  NULL: >>%LIS% 2>&1
%IM%compare -metric MAE -format "  mae1820=" ccy_%METH%_000018.miff ccy_%METH%_000020.miff  NULL: >>%LIS% 2>&1
%IM%convert ccy_%METH%_000020.miff -format "\nmean20=%%[fx:mean]" info: >>%LIS%

type %LIS%

colCyclStr.bat

rem Given %1 is a color cycling method and %2 is a percentage of cycle,
rem returns ccsImStr, a string to implement it.

goto %1

:null
set ccsImStr=
@exit /B 0

:am
set ccsImStr=-evaluate AddModulus %2%%%%
@exit /B 0

:am2
for /F "usebackq" %%L in (`%IM%identify -format "ccsPC=%%[fx:2*(%2)]" xc:`) do set %%L
set ccsImStr=-evaluate AddModulus %ccsPC%%%%%
@exit /B 0

:amsol
set ccsImStr=-evaluate AddModulus %2%%%% -solarize 50%% -evaluate Multiply 2
@exit /B 0

:amneg
set ccsPC=%2
for /F "usebackq" %%L in (`%IM%identify -format "ccsNEGPC=%%[fx:100-(%2)]" xc:`) do set %%L
set ccsImStr=^( -clone 0 -evaluate AddModulus !ccsPC!%%%% ^) ^
  ^( -clone 1 -negate ^) ^
  ^( -clone 0 -threshold !ccsNEGPC!%%%% ^) ^
  -delete 0 -compose Over -composite
@exit /B 0

:amnegaa
set ccsPC=%2
for /F "usebackq" %%L in (`%IM%identify -format "ccsNEGPC=%%[fx:100-(%2)]" xc:`) do set %%L
set ccsImStr=^( -clone 0 -evaluate AddModulus !ccsPC!%%%% ^) ^
  ^( -clone 1 -negate ^) ^
  ^( -clone 0 -threshold !ccsNEGPC!%%%% ^) ^
  -delete 0 -compose Over -composite ^
  -auto-level -auto-gamma
@exit /B 0

:amneg2
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsPC=%%[fx:%2<50?2*(%2):200-2*(%2)]\nccsNEGPC=%%[fx:(%2<50?100-2*(%2):2*(%2)-100)-0.00001]" ^
  xc:`) do set %%L
set ccsImStr=^( -clone 0 -evaluate AddModulus !ccsPC!%%%% ^) ^
  ^( -clone 1 -negate ^) ^
  ^( -clone 0 -threshold !ccsNEGPC!%%%% ^) ^
  -delete 0 -compose Over -composite
@exit /B 0

:amneg2aa
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsPC=%%[fx:%2<50?2*(%2):200-2*(%2)]\nccsNEGPC=%%[fx:(%2<50?100-2*(%2):2*(%2)-100)-0.00001]" ^
  xc:`) do set %%L
set ccsImStr=^( -clone 0 -evaluate AddModulus !ccsPC!%%%% ^) ^
  ^( -clone 1 -negate ^) ^
  ^( -clone 0 -threshold !ccsNEGPC!%%%% ^) ^
  -delete 0 -compose Over -composite ^
  -auto-level -auto-gamma
@exit /B 0

:amneg3
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsPC=%%[fx:2*(%2)]\nccsNEGPC=%%[fx:%2<50?100-2*(%2):200-2*(%2)]\nccsIsNEG=%%[fx:%2<50?0:1]" ^
  xc:`) do set %%L
if !ccsIsNEG!==1 (set ccsNEG=-negate) else set ccsNEG=
set ccsImStr=^( -clone 0 -evaluate AddModulus !ccsPC!%%%% !ccsNEG! ^) ^
  ^( -clone 1 -negate ^) ^
  ^( -clone 0 -threshold !ccsNEGPC!%%%% ^) ^
  -delete 0 -compose Over -composite
@exit /B 0

:amneg3n
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsPC=%%[fx:200-2*(%2)]\nccsNEGPC=%%[fx:(%2>=50?2*(%2)-100:2*(%2))-0.00001]\nccsIsNEG=%%[fx:%2>=50?0:1]" ^
  xc:`) do set %%L
if !ccsIsNEG!==1 (set ccsNEG=-negate) else set ccsNEG=
set ccsImStr=^( -clone 0 -evaluate AddModulus !ccsPC!%%%% !ccsNEG! ^) ^
  ^( -clone 1 -negate ^) ^
  ^( -clone 0 -threshold !ccsNEGPC!%%%% ^) ^
  -delete 0 -compose Over -composite
@exit /B 0

:amneg3aa
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsPC=%%[fx:2*(%2)]\nccsNEGPC=%%[fx:%2<50?100-2*(%2):200-2*(%2)]\nccsIsNEG=%%[fx:%2<50?0:1]" ^
  xc:`) do set %%L
if !ccsIsNEG!==1 (set ccsNEG=-negate) else set ccsNEG=
set ccsImStr=^( -clone 0 -evaluate AddModulus !ccsPC!%%%% !ccsNEG! ^) ^
  ^( -clone 1 -negate ^) ^
  ^( -clone 0 -threshold !ccsNEGPC!%%%% ^) ^
  -delete 0 -compose Over -composite ^
  -auto-level -auto-gamma
@exit /B 0

:sin
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsDEG=%%[fx:(%2)*3.6-90]" ^
  xc:`) do set %%L
set ccsImStr=-function sinusoid 1,!ccsDEG!,0.5,0.5
@exit /B 0

:sin2
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsDEG=%%[fx:(%2)*3.6-90]" ^
  xc:`) do set %%L
set ccsImStr=-function sinusoid 0.5,!ccsDEG!,0.5,0.5
@exit /B 0

:sin2aa
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsDEG=%%[fx:(%2)*3.6-90]" ^
  xc:`) do set %%L
set ccsImStr=-function sinusoid 0.5,!ccsDEG!,0.5,0.5 -auto-level -auto-gamma
@exit /B 0

:sinarc
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsDEG=%%[fx:(%2)*3.6-90]" ^
  xc:`) do set %%L
set ccsImStr=-function sinusoid 1,!ccsDEG!,0.5,0.5 -function arcsin 1
@exit /B 0

:sinarc2
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsDEG=%%[fx:(%2)*3.6-90]" ^
  xc:`) do set %%L
set ccsImStr=-function sinusoid 0.5,!ccsDEG!,0.5,0.5 -function arcsin 1
@exit /B 0

:sinarc2n
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsDEG=%%[fx:270-(%2)*3.6]" ^
  xc:`) do set %%L
set ccsImStr=-function sinusoid 0.5,!ccsDEG!,0.5,0.5 -function arcsin 1
@exit /B 0

:sinarc2o
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsDEG1=%%[fx:(%2)*3.6-90]\nccsDEG2=%%[fx:270-(%2)*3.6]" ^
  xc:`) do set %%L
set ccsImStr=^
  ^( -clone 0 ^
     -function sinusoid 0.5,!ccsDEG1!,0.5,0.5 ^
     -function arcsin 1 ^
  ^) ^
  ^( -clone 0 ^
     -function sinusoid 0.5,!ccsDEG2!,0.5,0.5 ^
     -function arcsin 1 ^
  ^) ^
  ^( -clone 0 ^
     -evaluate And 1 -fill White +opaque Black ^
  ^) ^
  -delete 0 ^
  -compose Over -composite
@exit /B 0

:sinarc2aa
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsDEG=%%[fx:(%2)*3.6-90]" ^
  xc:`) do set %%L
set ccsImStr=-function sinusoid 0.5,!ccsDEG!,0.5,0.5 -function arcsin 1 -auto-level -auto-gamma
@exit /B 0

:lev1
for /F "usebackq" %%L in (`%IM%identify ^
  -format "ccsPC=%%[fx:%2<50?2*(%2):200-2*(%2)]\nccsPC2=%%[fx:%2<50?100-2*(%2):2*(%2)-100]\n" ^
  xc:`) do set %%L
set ccsImStr=+level !ccsPC!%%,!ccsPC2!%%
@exit /B 0

:lev2
set ccsFORM=^
ccsPCL=%%[fx:%2^<50?(%2):100-(%2)]\n^
ccsPCH=%%[fx:%2^<50?3*(%2):300-3*(%2)]\n^
ccsPCH2=%%[fx:%2^<50?100-(%2):(%2)]\n^
ccsPCL2=%%[fx:%2^<50?100-3*(%2):3*(%2)-200]

for /F "usebackq" %%L in (`%IM%identify ^
  -format "!ccsFORM!" ^
  xc:`) do set %%L
set ccsImStr=^( -clone 0 ^
     +level !ccsPCL!%%%%,!ccsPCL2!%%%% ^) ^
  ^( -clone 0 +level !ccsPCH!%%%%,!ccsPCH2!%%%% ^) ^
  ^( -clone 0 -threshold 50%%%% ^) ^
  -delete 0 -compose Over -composite
@exit /B 0

:lev3
set ccsFORM=^
ccsPCL=%%[fx:%2^<50?2*(%2):200-2*(%2)]\n^
ccsPCL2=%%[fx:%2^<50?100-4*(%2):4*(%2)-300]\n^
ccsPCH=%%[fx:%2^<50?4*(%2):400-4*(%2)]\n^
ccsPCH2=%%[fx:%2^<50?100-2*(%2):2*(%2)-100]

for /F "usebackq" %%L in (`%IM%identify ^
  -format "!ccsFORM!" ^
  xc:`) do set %%L
set ccsImStr=^( -clone 0 ^
     +level !ccsPCL!%%%%,!ccsPCL2!%%%% ^) ^
  ^( -clone 0 +level !ccsPCH!%%%%,!ccsPCH2!%%%% ^) ^
  ^( -clone 0 -threshold 50%%%% ^) ^
  -delete 0 ^
  -compose Over -composite -auto-level
@exit /B 0

colCyclList.bat

I don't provide the source or binary of chStrs.exe, which here simply strips any colons (":") from the piped stream.

@rem  Write a list of available colour cycle methods to stdout

@findstr "^:" %PICTBAT%colCyclStr.bat |chStrs /p0 /i- /o- /f":"

colCyclTst.bat

rem Given image %1,
rem for each colour cycling method, makes test GIF named cct_!METH!.gif
rem Also creates an HTM file, name %2.
rem %3 optional prefix for GIF files. [cct]

@if "%1"=="" findstr /B "rem @rem" %~f0 & exit /B 1

@setlocal

@call echoOffSave

call %PICTBAT%setInOut %1 pd

set HTM=%2
if "%HTM%"=="" set HTM=cct_gifs.h1

set GIF_PREF=%3
if "%GIF_PREF%"=="" set GIF_PREF=cct


set TMPDIR=%TEMP%\

del %TMPDIR%cct_*.miff 2>nul

set METH_LIST=%TEMP%\cctList.lis
del %HTM% 2>nul

set TXT=%TMPDIR%cct_text.png


for /F "usebackq" %%L in (`%IM%identify ^
  -format "WW=%%w\nHH=%%h\nWT=%%[fx:max(w*0.9,50)]\nHT=%%[fx:max(h*0.9,20)]" ^
  %INFILE%`) do set %%L


echo ^<h1^>Color cycle tests^</h1^> >%HTM%
echo ^<p^>This page is automatically created by the script ^<tt^>colCyclTst.bat^</tt^>. >>%HTM%
echo See ^<a href="colcycl.htm#cct"^>Colour cycling: tests^</a^>.^</p^> >>%HTM%

set FOR_ARGS=0,2,99

call %PICTBAT%colCyclList >%METH_LIST%

for /F %%M in (%METH_LIST%) do (
  set METH=%%M
  echo method=!METH!

  %IM%convert ^
    -size %WT%x%HT% label:!METH! ^
    -trim +repage ^
    -bordercolor White -border 3 ^
    -size %WW%x%HH% xc: ^
    +swap ^
    -gravity South -append ^
    %TXT%

  for /L %%i in (%FOR_ARGS%) do (

    set LZ=000000%%i
    set LZ=!LZ:~-6!

    call %PICTBAT%colCyclStr !METH! %%i

    %IM%convert ^
      %INFILE% ^
      !ccsImStr! ^
      %TMPDIR%cct_!LZ!.miff
  )

  %IM%convert ^
    -loop 0 ^
    %TXT% ^
    null: ^
    %TMPDIR%cct_*.miff ^
    -gravity North ^
    -layers composite ^
    +repage ^
    -layers optimize ^
    %GIF_PREF%_!METH!.gif

  del %TMPDIR%cct_*.miff

  echo ^<img src="%GIF_PREF%_!METH!.gif" /^> >>%HTM%
)

setlocal DisableDelayedExpansion
echo ^<!--Page-version v1.0 23-Sep-2015--^> >>%HTM%
endlocal

echo Created cct_*.gif and %HTM%

call echoRestore

@endlocal & set pdOUTFILE=%HTM%

nseCycl.bat

This creates a single web page showing all colour cycling methods for all +noise types.

set OUT_HTM=nc_htm.htm

del %OUT_HTM%

for /F "usebackq" %%N in (`%IM%convert -list noise`) do (
  echo %%N

  %IM%convert ^
    -size 200x200 xc:gray^(50%%^) +noise %%N ^
    nc_%%N.png

  set HTM=htm_%%N

  call %PICTBAT%colCyclTst nc_%%N.png !HTM! nc_%%N

  echo ^<h2^>%%N^</h2^> >>%OUT_HTM%

  type !HTM! >>%OUT_HTM%

)

All images on this page were created by the commands shown, using:

%IM%identify -version
Version: ImageMagick 6.9.1--6 Q16 x64 2015-06-20 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib cairo freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib

To improvpenMP Delegates (built-in): bzlib cairo freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib

To improve internet download speeds, some images may have been automatically converted (by ImageMagick, of course) from PNG to JPG.

Source file for this web page is colcycl.h1. To re-create this web page, run "procH1 colcycl".


This page, including the images, is my copyright. Anyone is permitted to use or adapt any of the code, scripts or images for any purpose, including commercial use.

Anyone is permitted to re-publish this page, but only for non-commercial use.

Anyone is permitted to link to this page, including for commercial use.


Page version v1.0 18-September-2015.

Page created 14-Oct-2015 05:59:03.

Copyright © 2015 Alan Gibson.