reb
Class REB_FibersThings

Object
  extended by common:Generic
      extended by common:Coloring
          extended by common:DirectColoring
              extended by reb:REB_FibersThings

class 
DirectColoring:REB_FibersThings

Based upon the divergent formula code of Jussi Härkönen, but for convergent formulas.

Direct Coloring method that looks like triange inequality average or curvature average but which works specifically for convergent fractals like Cayley Julia and Cayley Mandelbrot. The smoothing code is modified from Linas Vepstas and Damien Jones. Some of the code ideas came from the work of Jussi Härkönen. Corrections to the fractional iteration count made by Jussi Härkönen. The class supports trapshape textures and colortrap coloring, including image import.


Ultra Fractal Source

Toggle UF Source Code Display

 class REB_FibersThings(common.ulb:DirectColoring) {
 ; Based upon the divergent formula code of Jussi Härkönen, but for convergent formulas. <br>
 ; <p>
 ;  Direct Coloring method that looks like triange inequality average
 ; or curvature average but which works specifically for convergent
 ; fractals like Cayley Julia and Cayley Mandelbrot. The smoothing code is
 ; modified from Linas Vepstas and Damien Jones. Some of the code ideas came
 ; from the work of Jussi Härkönen. Corrections to the fractional iteration
 ; count made by Jussi Härkönen. The class supports trapshape textures and
 ; colortrap coloring, including image import.
 public:
   import "common.ulb"
   import "dmj5.ulb"
 
   ; constructor
   func REB_FibersThings(Generic pparent)
     DirectColoring.DirectColoring(pparent)
     m_Texture = new @ftexture(this)
     m_MergeColor = new @f_colormerge(this)
     m_TrapColor = new @f_trapcolor(this)
   endfunc
   
   ; initialize the objects
   func Init(complex pz, complex ppixel)
     DirectColoring.Init(pz, ppixel)
     m_Texture.Init(pz)
     m_TrapColor.Init(pz)
     ci = (0,1)
     rot = ci^(@ang/90)
     skew = ci^(@skew/90)
     prot = ci^(@prot/90)
     z2 = 0
     zold = (0,0)
     zold2 = (0,0)
     i = 0
     a = 0
     a2 = 0
     f = 0
     ztype = (0,0)
     if @zmod == "point" || @zmod == "rot point"
       ztype = @spoint
     endif
     power = 0
     m_empty = true
   endfunc
   
   func Iterate(complex pz)
     DirectColoring.Iterate(pz)
     if (@mtile == "Fixed Iteration" && m_iterations == @iternum) || \
        (@mtile == "Cabs(z)" && @bailout < 1 && cabs(zold-pz) > @tcabs) || \
        (@mtile == "Cabs(z)" && @bailout > 1 && cabs(pz) < @tcabs) || @mtile == "None"
       ptexture = m_Texture.Iterate(pz)
       if @v_fibersandthings < 101
         m_img = new @f_image(0)
         m_empty = m_img.GetEmpty()
       endif
       if @v_fibersandthings >= 101
         if @f_trapcolor == ColorTrapNoColor
           m_empty = true
         else
           m_color = m_TrapColor.Iterate(pz)
           m_empty = false
         endif
       else
         if !m_empty
           complex m_zi = (0,1)^(@iangle/90.0)
           complex m_zs = (0,1)^(@sangle/90.0)
           complex pzi = pz
           ; aspect ratio
           pzi = real(pzi) + flip(imag(pzi)*@iaspect)
           ; rotation
           pzi = pzi*m_zi
           ; skew
           pzi = real(pzi)*m_zs + flip(imag(pzi))
           float width =  m_img.getWidth()
           float height = m_img.getHeight()
           complex pzi = (pzi+(100,100))*@scale*1000
           complex c = @adjx-2*((real(pzi) % (width-1))/width) + \
              flip(@adjy-2*((imag(pzi) % (height-1))/height))
           m_color = m_img.getColor(c)
         endif
       endif
     endif
     a2 = a
     if @flavor != "smooth"
       z2 = real(pz)*skew + flip(imag(pz))
       z2 = z2*rot
     else
       z2 = pz
     endif
     if @zmod == "rot point" && |ztype| != 0
       ztype = ztype*prot
     endif
     if @flavor == "Standard"
       a = a + cabs(fn1(@density*(atan2(fn2(z2-zold-ztype)))+@colorshift*#pi))
     elseif @flavor == "Curvature"
       a = a + cabs(fn1(@density*(atan2(fn2((z2-zold)/(zold-zold2)-ztype)))+@colorshift*#pi))
     elseif @flavor == "Decomposition"
       a = a + cabs(fn1(@density*(atan2(fn2(z2-ztype)))+@colorshift*#pi))
     elseif @flavor == "Decomp multiply"
       a = a + cabs(fn1(@density*(atan2(fn2(z2*zold-ztype)))+@colorshift*#pi))
     elseif @flavor == "Decomp divide"
       a = a + cabs(fn1(@density*(atan2(fn2(z2/zold-ztype)))+@colorshift*#pi))
     elseif @flavor == "Magnitude"
       a = a + cabs(fn1(@density*(cabs(fn2(z2-ztype)))+@colorshift*#pi))
     elseif @flavor == "Magnitude 2"
       a = a + cabs(fn1(@density*(cabs(fn2(z2-zold-ztype)))+@colorshift*#pi))
     elseif @flavor == "Magnitude 3"
       a = a + cabs(fn1(@density*(|fn2(z2-ztype)|)+@colorshift*#pi))
     elseif @flavor == "Magnitude 4"
       a = a + cabs(fn1(@density*(|fn2(z2-zold-ztype)|)+@colorshift*#pi))
     endif
     zold2 = zold
     zold = z2
     i = i + 1
   endfunc
 
   color func Result(complex pz)
     power = log(|pz - zold|) / log(|zold - zold2|)
     z2 = real(pz)*skew + flip(imag(pz))
     z2 = z2*rot
     f = (log(log(@bailout)) - log(log(1/(|(zold - z2)|))))/log(power)
     a = a/i
     a2 = a2/(i-1)
     color return_color = gradient(a2 + (a-a2)*(f+1) + ptexture*@txamt)
     if @v_fibersandthings >= 102
       if @ahue
         m_color = hsla(hue(m_color), sat(m_color), lum(m_color), \
                       hue(m_color)/6*alpha(m_color))
       endif
       if @alum
         m_color = hsla(hue(m_color), sat(m_color), lum(m_color), \
                       lum(m_color)*alpha(m_color))
       endif
       if @asat
         m_color = hsla(hue(m_color), sat(m_color), lum(m_color), \
                       sat(m_color)*alpha(m_color))
       endif
     endif
     if @flavor == "Smooth"
       return_color = gradient(0.05*(m_iterations+f) + ptexture*@txamt)
     endif
     color temp = rgba(0,0,0,0)
     if !m_empty
       if @nmerge == 1
         temp = m_color
         m_color = return_color
         return_color = temp
       endif
       return m_MergeColor.FullMerge(return_color, m_color, @opacity)
     endif
     if @v_fibersandthings >= 103
       float br = (0.5-@p_bright)*2
       int sign = 0
       if br < 0
         sign = -1
       else
         sign = 1
       endif
       br = br^2*sign
       float cr = (@p_contrast-0.5)*2
       if cr < 0
         sign = -1
       else
         sign = 1
       endif
       cr = cr^2*sign+1
       float st = (0.5-@p_sat)*2
       if st < 0
         sign = -1
       else
         sign = 1
       endif
       st = st^2*sign*2
       float hu = @p_hue*6
       float rd = red(return_color)-br
       float gr = green(return_color)-br
       float bl = blue(return_color)-br
       if rd > 1
         rd = 1
       endif
       if gr > 1
         gr = 1
       endif
       if bl > 1
         bl = 1
       endif
       if rd < 0
         rd = 0
       endif
       if gr < 0
         gr = 0
       endif
       if bl < 0
         bl = 0
       endif
       return_color = rgba(rd,gr,bl,alpha(return_color))
       rd = red(return_color)^cr
       gr = green(return_color)^cr
       bl = blue(return_color)^cr
       return_color = rgba(rd,gr,bl,alpha(return_color))
       float satval = sat(return_color)-st
       if satval > 1
         satval = 1
       endif
       if satval < 0
         satval = 0
       endif
       return_color = hsla(hue(return_color), satval, lum(return_color), alpha(return_color))
       float hueval = (hue(return_color)+hu) % 6
       return_color = hsla(hueval, sat(return_color), lum(return_color), alpha(return_color))
       if @p_poster
         float rd = floor(red(return_color)*@p_chan)/@p_chan + 0.5/@p_chan
         float gr = floor(green(return_color)*@p_chan)/@p_chan + 0.5/@p_chan
         float bl = floor(blue(return_color)*@p_chan)/@p_chan  + 0.5/@p_chan
         return_color = rgba(rd,gr,bl,alpha(return_color))
       endif
       if @p_gray
          float gry = 0.3 * red(return_color)+ 0.59 * green(return_color) + 0.11 * blue(return_color)
          return_color = rgba(gry,gry,gry,alpha(return_color))
       endif
       if @p_solar
         float rd = red(return_color)
         float gr = green(return_color)
         float bl = blue(return_color)
         if rd > @p_thresh
           rd = 1-rd
         endif
         if gr > @p_thresh
           gr = 1-gr
         endif
         if bl > @p_thresh
           bl = 1-bl
         endif
         return_color = rgba(rd,gr,bl,alpha(return_color))
       endif
       if @p_bw
         float gry = 0.3 * red(return_color)+ 0.59 * green(return_color) + 0.11 * blue(return_color)
         if gry < @p_bwt
           return_color = rgba(0,0,0,alpha(return_color))
         else
           return_color = rgba(1,1,1,alpha(return_color))
         endif
       endif
     endif
     return return_color
   endfunc
 
 protected:
   TrapShape m_Texture
   float ptexture
   ImageImport m_img
   bool m_empty
   color m_color
   DefaultColorMerge m_MergeColor
   ColorTrap m_TrapColor
   complex ci
   complex rot
   complex skew
   complex prot
   complex z2
   complex zold
   complex zold2
   int i
   float a
   float a2
   float f
   complex ztype
   float power
 
 default:
   title = "Fibers and Things"
   int param v_fibersandthings
     caption = "Version (Fibers And Things)"
     default = 103
     hint = "This version parameter is used to detect when a change has been made to the formula that is incompatible with the previous version. When that happens, this field will reflect the old version number to alert you to the fact that an alternate rendering is being used."
     visible = @v_fibersandthings < 103
   endparam
   heading
     text = "This coloring method was designed for use with convergent fractals. \
            It was specifically designed for the convergent formulas in reb.ufm \
            and may not work with all convergent fractals. The bailout should \
            be the same for both the coloring formula and the ufm. Some coding ideas \
            came from the work of Jussi Härkönen as did corrections \
            to the convergent smoothing algorithm."
   endheading
   param bailout
     caption = "bailout"
     default = 1e-12
   endparam
   param flavor
     caption = "flavor"
     default = 0
     enum = "standard" "curvature" "decomposition" "decomp multiply" \
            "decomp divide" "magnitude" "magnitude 2" "magnitude 3" \
            "magnitude 4" "smooth"
   endparam
   param zmod
     caption ="z modifier"
     enum = "none" "point" "rot point"
     default = 0
     visible = @flavor != "smooth"
   endparam
   float param prot
     caption = "point rotation"
     default = 0
     visible = @flavor != "smooth"&&@zmod == "rot point"
   endparam
   complex param spoint
     caption = "point val"
     default = (1,-1)
     visible = @flavor != "smooth"&&(@zmod == "point" || @zmod == "rot point")
   endparam
   float param ang
     caption ="z rotation"
     default = 0
     visible = @flavor != "smooth"
   endparam
   float param skew
     caption ="z skew"
     default = 0
     visible = @flavor != "smooth"
   endparam
   func fn1
     caption = "fiber function #1"
     default = cos()
     visible = @flavor != "smooth"
   endfunc
   func fn2
     caption = "fiber function #2"
     default = ident()
     visible = @flavor != "smooth"
   endfunc
   complex param density
     caption = "fiber density"
     default = 10.0
     visible = @flavor != "smooth"
   endparam
   float param colorshift
     caption = "color shift"
     default = 0.0
     min = 0
     max = 1
     visible = @flavor != "smooth"
   endparam
   float param txamt
     caption = "Texture amount"
     default = 0.0
     visible = @fTexture != DMJ_TrapShapeFlat
   endparam
   heading
     text = "The 'Tile method' applies to both textures and images. Fixed Iteration \
             will give a smoother tiling, while 'Cabs(z)' will more consistently \
             follow the fractal shape."
     visible = @fTexture != DMJ_TrapShapeFlat || @f_trapcolor != ColorTrapNoColor
   endheading
   param mtile
     caption = "Tile method"
     default = 1
     enum = "None" "Fixed Iteration" "Cabs(z)"
     visible = @fTexture != DMJ_TrapShapeFlat || @f_trapcolor != ColorTrapNoColor
   endparam
   int param iternum
     caption = "Iter number"
     default = 1
     hint = "Changes the texture/image mapping."
     visible = (@fTexture != DMJ_TrapShapeFlat || @f_trapcolor != ColorTrapNoColor) && \
               @mtile == "Fixed iteration"
   endparam
   float param tcabs
     caption = "Cabs limit"
     default = 0.5
     visible = @mtile == "Cabs(z)"
     hint = "Changes the texture/image mapping."
     visible = @fTexture != DMJ_TrapShapeFlat || @f_trapcolor != ColorTrapNoColor
   endparam
   TrapShape param fTexture
     caption = "Texture"
     default = DMJ_TrapShapeFlat
     hint = "Use TrapShape plugins as textures."
   endparam
   heading
     caption = "Images and Colorings"
   endheading
   ColorTrap param f_trapcolor
     caption = "Colors"
     default = ColorTrapNoColor
 ;    expanded = false
     hint = "A trap shape that is used as a color or pattern."
     visible = @v_fibersandthings >= 101
   endparam
   ImageImport param f_image
     caption = "Image"
     default = ImageImport
     visible = @v_fibersandthings < 101
   endparam
   float param scale
     caption = "Image scale"
     default = 1
     min = 0
     hint = "Changes the scale of the image."
     visible = @v_fibersandthings < 101
   endparam
   float param adjx
     caption = "Tile adj x"
     default = 0.99
     min = 0.9
     max = 1.1
     visible = @v_fibersandthings < 101
   endparam
   float param adjy
     caption = "Tile adj y"
     default = 0.999
     min = 0.9
     max = 1.1
     visible = @v_fibersandthings < 101
   endparam
   float param iaspect
     caption = "Image aspect"
     default = 1.0
     visible = @v_fibersandthings < 101
   endparam
   float param iangle
     caption = "Image rotation"
     default = 0
     hint = "Rotates the image"
     visible = @v_fibersandthings < 101
   endparam
   float param sangle
     caption = "Image skew"
     default = 0
     hint = "Skews the image"
     visible = @v_fibersandthings < 101
   endparam
   DefaultColorMerge param f_colormerge
     caption = "Color Merge"
     default = DefaultColorMerge
     visible = @v_fibersandthings < 101 || (@v_fibersandthings >= 101 \
               && @f_trapcolor != ColorTrapNoColor)
   endparam
   param nmerge
     caption = "Merge order"
     default = 0
     enum = "Image on Top" "Image on Bottom"
     visible = @v_fibersandthings < 101 || (@v_fibersandthings >= 101 \
               && @f_trapcolor != ColorTrapNoColor)
   endparam
   float param opacity
     caption = "Merge Opacity"
     default = 0.2
     visible = @v_fibersandthings < 101 || (@v_fibersandthings >= 101 \
               && @f_trapcolor != ColorTrapNoColor)
   endparam
   heading
     text = "Make image transparent by: "
     visible = @v_fibersandthings >= 102 && @f_trapcolor != ColorTrapNoColor
   endheading
   bool param ahue
     caption = "Hue value"
     default = false
     visible = @v_fibersandthings >= 102 && \
             @f_trapcolor != ColorTrapNoColor
   endparam
   bool param alum
     caption = "Luminance value"
     default = false
     visible = @v_fibersandthings >= 102  && \
             @f_trapcolor != ColorTrapNoColor
   endparam
   bool param asat
     caption = "Saturation value"
     default = false
     visible = @v_fibersandthings >= 102  && \
             @f_trapcolor != ColorTrapNoColor
   endparam
   heading
     caption = "Special Effects"
     visible = @v_fibersandthings >= 103
   endheading
   float param p_bright
     caption = "Brightness"
     default = 0.5
     min = 0
     max = 1
     visible = @v_fibersandthings >= 103
   endparam
   float param p_contrast
     caption = "Contrast"
     default = 0.5
     min = 0
     max = 1
     visible = @v_fibersandthings >= 103
   endparam
   float param p_sat
     caption = "Saturation"
     default = 0.5
     min = 0
     max = 1
     visible = @v_fibersandthings >= 103
   endparam
   float param p_hue
     caption = "Hue"
     default = 0.0
     min = 0
     max = 1
     visible = @v_fibersandthings >= 103
   endparam
   bool param p_gray
     caption = "Grayscale"
     default = false
     visible = @v_fibersandthings >= 103
   endparam
   bool param p_solar
     caption = "Solarize"
     default = false
     visible = @v_fibersandthings >= 103
   endparam
   float param p_thresh
     caption = "Threshold"
     default = 0.5
     visible = @p_solar && @v_fibersandthings >= 103
   endparam
   bool param p_poster
     caption = "Posterize"
     default = false
     visible = @v_fibersandthings >= 103
   endparam
   int param p_chan
     caption = "Colors per channel"
     default = 4
     min = 2
     visible = @p_poster && @v_fibersandthings >= 103
   endparam
   bool param p_bw
     caption = "Black and White"
     default = false
     visible = @v_fibersandthings >= 103
   endparam
   float param p_bwt
     caption = "Threshold"
     default = 0.5
     min = 0
     max = 1
     visible = @p_bw && @v_fibersandthings >= 103
   endparam
 
 }
 


Constructor Summary
REB_FibersThings()
           
REB_FibersThings(Generic pparent)
          constructor
 
Method Summary
 void Init(complex pz, complex ppixel)
          initialize the objects
 void Iterate(complex pz)
          Process the next value in the sequence
 color Result(complex pz)
          Produce a resulting color index after a sequence is finished
 
Methods inherited from class common:DirectColoring
IsSolid
 
Methods inherited from class common:Coloring
GetPixel, IsGradient
 
Methods inherited from class common:Generic
GetParent
 
Methods inherited from class Object
 

Constructor Detail

REB_FibersThings

public REB_FibersThings(Generic pparent)
constructor


REB_FibersThings

public REB_FibersThings()
Method Detail

Init

public void Init(complex pz,
                 complex ppixel)
initialize the objects

Overrides:
Init in class DirectColoring
Parameters:
pz - first value for the sequence; for a normal coloring formula, this will be #z
ppixel - seed value for the sequence; for a normal coloring formula, this will be #pixel

Iterate

public void Iterate(complex pz)
Description copied from class: DirectColoring
Process the next value in the sequence

As long as the sequence has not bailed out, this function will be continually called to produce sequence values. Note that such processing generally will not know in advance precisely how long the sequence is, and should be prepared to deal with sequences of arbitrary length.

Your coloring may determine at some point that a solid color should be used rather than an index value.

Overrides:
Iterate in class DirectColoring
Parameters:
pz - next value in the sequence; corresponds to #z in a coloring formula

Result

public color Result(complex pz)
Description copied from class: DirectColoring
Produce a resulting color index after a sequence is finished

This corresponds to the final: section in a coloring formula. Once it is called, no further calls to Iterate() should be made without calling Init() first.

Overrides:
Result in class DirectColoring
Returns:
the gradient index (corresponding to #index in a coloring formula)