Slider Scale Graphics
Posted: Mon Aug 24, 2020 11:05 pm
I recently needed some slider scale graphics so I thought I'd have a go at drawing my own with Ruby.
There are probably better ways of doing this but it works ok for me.
here is the result:
Hope it's as useful to others as it is to me
There are probably better ways of doing this but it works ok for me.
here is the result:
- Code: Select all
def init
@background_brush = Brush.new Color.new 45
end
def slider_scale(x,y,w,h)
d_fine = 0.75 # distance between lines
d_coarse = d_fine * 5 # coarse line every n
track_brush = Brush.new Color.new 0
line_brush = Brush.new Color.new 180
xs =[x,x - 0.125,x - 0.25]
ys = [y,y + 0.125,y + 0.25]
ws = [0.125,0.375,0.625]
hs = [h,h - 0.25,h - 0.5]
cols = [track_brush] * 3
a = (y + 0.5..y + h - 0.25).step(d_fine).to_a[1...-1]
b = (y + 0.5..y + h - 0.25).step(d_coarse).to_a
lxsf = [x - (w * 0.375)] * a.size
lysf = a
lwsf = [w * 0.375 - 0.75] * a.size
lhsf = [0.125] * a.size
lcolsf = [line_brush] * a.size
rxsf = [x + 0.875] * a.size
rysf = a
rwsf = [w * 0.375 - 0.875] * a.size
rhsf = [0.125] * a.size
rcolsf = [line_brush] * a.size
lxsc = [x - (w * 0.5)] * b.size
lysc = b
lwsc = [w * 0.5 - 0.75] * b.size
lhsc = [0.125] * b.size
lcolsc = [line_brush] * b.size
rxsc = [x + 0.875] * b.size
rysc = b
rwsc = [w * 0.5 - 0.875] * b.size
rhsc = [0.125] * b.size
rcolsc = [line_brush] * b.size
d = lxsf + xs + rxsf + lxsc + rxsc
e = lysf + ys + rysf + lysc + rysc
f = lwsf + ws + rwsf + lwsc + rwsc
g = lhsf + hs + rhsf + lhsc + rhsc
h = lcolsf + cols + rcolsf + lcolsc + rcolsc
h.zip(d,e,f,g)
end
def draw v
v.setSmoothingMode 0
v.drawRectangle @background_brush, [0,0,v.width,v.height]
slider_scale(8,5,4.25,28).each do|e|
v.drawRectangle e.first,e.last(4)
end
slider_scale(20,5,5.125,35).each do|e|
v.drawRectangle e.first,e.last(4)
end
slider_scale(32,5,8.375,43).each do|e|
v.drawRectangle e.first,e.last(4)
end
end
Hope it's as useful to others as it is to me