tips_clr_030 - spoolkitamura/nyle-doc-jp GitHub Wiki

ある座標の色を調べるには?

pixelメソッドまたは pixel?メソッドを使って調べることができます。

pixelは指定した座標の色情報を得るときに使い、
pixel?は指定した座標の色情報が調べたい色と同じかどうかを判定するときに使います。

[関連情報]
pixel
pixel-
color_pixel.rb の紹介
color_pixel.rb

 

pixelメソッドを使った色の取得

require 'nyle'

class Screen < Nyle::Screen
  def initialize
    super(240, 200, {bgcolor: :IVORY})
    @x = 50
    @y = 50
  end

  def draw
    Nyle.draw_rect(10, 10, 220, 90, {color: :BLUE, fill: true})
    Nyle.draw_text(10, 160, "座標(#{@x}, #{@y})の位置の色は" , {size: 16})
    Nyle.draw_text(10, 180, "#{Nyle.pixel(@x, @y)}です",       {size: 16})
  end
end

Screen.new.show_all
Nyle.main

[実行結果]

 


pixel?メソッドを使った色のチェック

require 'nyle'

class Screen < Nyle::Screen
  def initialize
    super(240, 200, {bgcolor: :IVORY})
    @x     = 50
    @y     = 50
    @color = :GREEN
  end

  def draw
    Nyle.draw_rect(10, 10, 220, 90, {color: :BLUE, fill: true})
    Nyle.draw_text(10, 160, "座標(#{@x}, #{@y})の位置の色は" , {size: 16})
    if Nyle.pixel?(@x, @y, @color)
      Nyle.draw_text(10, 180, "#{@color}です",           {size: 16})
    else
      Nyle.draw_text(10, 180, "#{@color}ではありません", {size: 16})
    end
  end
end

Screen.new.show_all
Nyle.main

[実行結果]