Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Sunday, 3 May 2015

Adding structure to processing examples (in ruby-processing)

Thinking about ruby-2.0 and structure got me thinking about processing color, but then I though about Struct and OStruct, as easy way to add structure to these processing sketches, see how this gives meaning to fill for example:-
Hue
# Hue is the color reflected from or transmitted through an object
# and is typically referred to as the name of the color (red, blue, yellow,
# etc.). Move the cursor vertically over each bar to alter its hue.
require 'ostruct'

attr_reader :bar_width, :color_array

def setup
  size 640, 360
  color_mode HSB, 360
  no_stroke
  @bar_width = 20
  @color_array = (0..width / bar_width).map do
    OpenStruct.new(hue: height, saturation: height, brightness: height)
  end
end

def draw
  color_array.each_with_index do |col, i|
    n = i * bar_width
    range = (n..n + bar_width)
    color_array[i].hue = mouse_y if range.include?(mouse_x)
    fill col.hue, col.saturation, col.brightness
    rect n, 0, bar_width, height
  end
end

Saturation
# Saturation is the strength or purity of the color and represents the
# amount of gray in proportion to the hue. A "saturated" color is pure
# and an "unsaturated" color has a large percentage of gray.
# Move the cursor vertically over each bar to alter its saturation.
require 'ostruct'

attr_reader :bar_width, :color_array

def setup
  size 640, 360
  color_mode HSB, 360
  no_stroke
  @bar_width = 20
  @color_array = (0..width / bar_width).map do |n|
    OpenStruct.new(hue: n * bar_width, saturation: height, brightness: height / 1.5)
  end
end

def draw
  color_array.each_with_index do |col, i|
    n = i * bar_width
    range = (n..n + bar_width)
    color_array[i].saturation = mouse_y if range.include? mouse_x
    fill col.hue, col.saturation, col.brightness
    rect n, 0, bar_width, height
  end
end

Brightness
# Brightness is the relative lightness or darkness of a color.
# Move the cursor vertically over each bar to alter its brightness.
require 'ostruct'

attr_reader :bar_width, :color_array

def setup
  size 640, 360
  no_stroke
  color_mode HSB, width, 100, height
  @bar_width = 20
  @color_array = (0..width / bar_width).map do |n|
    OpenStruct.new(hue: n * bar_width, saturation: 100, brightness: height)
  end
end

def draw
  color_array.each_with_index do |col, i|
    n = i * bar_width
    range = (n..n + bar_width)
    color_array[i].brightness = mouse_y if range.include? mouse_x
    fill col.hue, col.saturation, col.brightness
    rect n, 0, bar_width, height
  end
end

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2