Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rgbcell

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rgbcell

  • 0.7
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

RGBCell

RGBCell represents a color as the coordinates in the x-y-z coordinate system. Each of the three component color elements, red, green, and blue, have a float value from 0 to 255.

Creating an object without any params creates black:

1.  require 'rgbcell'
2.  color = RGBCell.new
3.  color.to_s    #=> #000000
4.  color.red     #=> 0.0
5.  color.green   #=> 0.0
6.  color.blue    #=> 0.0

Line 1 loads the RGBCell module. Line 2 create an RGBCell object. Because there are no parameters given, the object represents black. Line 3 show the stringification of the color such as might be used in CSS. Lines 4-6 show the numeric values of the three component colors, which are stored as floats.

There are several ways to indicate the color in RGBCell::new. One way is to give its three components as numeric values.

color = RGBCell.new(127, 255, 0)
color.to_s    #=> #7fff00

You can indicate the color using hex format:

color = RGBCell.new('#7fff00')
color.to_s    #=> #7fff00
color.red     #=> 127.0
color.green   #=> 255.0
color.blue    #=> 0.0

You can give one of the HTML named colors. All 140 official named HTML colors are available. See W3C's page for the complete list.

color = RGBCell.new('purple')
color.to_s    #=> #800080
color.red     #=> 128.0
color.green   #=> 0.0
color.blue    #=> 128.0

You can use 'random' to get a random color:

color = RGBCell.new('random')
color.to_s    #=> #dbd25b
color.red     #=> 219.0
color.green   #=> 210.0
color.blue    #=> 91.0

Finally, you can also set each component color individually. Notice in this example that an integer value is converted to float.

color = RGBCell.new()
color.red = 60       #=> 60.0
color.green = 230    #=> 210.0
color.blue = 91.005  #=> 91.0005
color.to_s           #=> #3ce65b

Distance

Because every color is a set of coordinates, the difference between two colors can be expressed as a distance. The distance is always zero or a positive number. For example, the following code gives the distance between red and chartreuse.

red = RGBCell.new('red')
chartreuse = RGBCell.new('chartreuse')
red.distance(chartreuse)  #=> 285.3226244096321

The same thing can be expressed with the minus operator.

puts red - chartreuse  #=> 285.3226244096321

The second color does not have to be an RGBCell object; it only needs to be an expression that can be used to create an RGBCell object.

puts red - 'green'  #=> 360.62445840513925

Average

Two or more colors can be averaged by calculating the midpoint of their coordinates using the average method. The result of the average is itself an RGBCell object.

red = RGBCell.new('red')
tomato = RGBCell.new('tomato')
avg = red.average(tomato)
puts avg.class  #=> RGBCell
puts avg.to_s   #=> #ff3100

You can pass in more than one color for averaging. You can use RGBCell objects or any object that can be used to create an RGBCell object.

tomato = RGBCell.new('tomato')
puts tomato.average('orange', 'olive') #=> #d48200

If you prefer not to create any colors at all, you can use the class method.

puts RGBCell.average('tomato', 'orange', 'olive') #=> #d48200

Install

gem install rgbcell

Author

Mike O'Sullivan mike@idocs.com

History

versiondatenotes
0.5June 25, 2020Initial upload.
0.6June 26, 2020Edits to documentation.
0.7June 26, 2020Another important edit to documentation.

FAQs

Package last updated on 26 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc