commit 3d9cf7858b53861d4e964dd32652d2b4cc803491 Author: Danila Fedorin Date: Tue Sep 18 23:06:12 2018 -0700 Create initial version of shard. diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..163eb75 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*.cr] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e29dae7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/docs/ +/lib/ +/bin/ +/.shards/ +*.dwarf + +# Libraries don't need dependency lock +# Dependencies will be locked in application that uses them +/shard.lock diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ffc7b6a --- /dev/null +++ b/.travis.yml @@ -0,0 +1 @@ +language: crystal diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e66fb36 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2018 Danila Fedorin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e536a2d --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# golden-color + +A simple shard to generate colors using the golden ratio method. + +## Installation + +Add this to your application's `shard.yml`: + +```yaml +dependencies: + golden-color: + git: https://dev.danilafe.com/DanilaFe/golden-color +``` + +## Usage + +The first step to using golden-color is to create a color generator. +The saturation and value parameters can be used to ajust the colors +that are produced. Additionally, the seed parameter can be modified +to change the starting hue. Colors can then be generated by repeated +calls to "generate". + +```crystal +require "golden-color" + +generator = GoldenColor::ColorGen.new saturation: 0.5, value: 0.95 +color = generator.generate +``` + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create a new Pull Request + +## Contributors + +- [DanilaFe](https://dev.danilafe.com/DanilaFe) Danila Fedorin - creator, maintainer diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..8d6fa50 --- /dev/null +++ b/shard.yml @@ -0,0 +1,9 @@ +name: golden-color +version: 0.1.0 + +authors: + - Danila Fedorin + +crystal: 0.26.1 + +license: MIT diff --git a/spec/golden-color_spec.cr b/spec/golden-color_spec.cr new file mode 100644 index 0000000..70b12d8 --- /dev/null +++ b/spec/golden-color_spec.cr @@ -0,0 +1,27 @@ +require "./spec_helper" + +describe GoldenColor do + describe ".from_rgb" do + it "Correctly creates a color from red, green and blue" do + c = GoldenColor::Color.from_rgb 0.0, 0.5, 1.0 + c.r.should eq 0.0 + c.g.should eq 0.5 + c.b.should eq 1.0 + end + + it "Correctly creates a color from hue, saturation and value" do + c = GoldenColor::Color.from_hsv 0.0, 0.5, 0.95 + c.r.should eq 0.95 + c.g.should eq 0.475 + c.b.should eq 0.475 + end + + it "Correctly reports a tuple of red, green, and blue" do + c = GoldenColor::Color.from_rgb 0.0, 0.5, 1.0 + r, g, b = c.rgb + r.should eq c.r + g.should eq c.g + b.should eq c.b + end + end +end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr new file mode 100644 index 0000000..c061b83 --- /dev/null +++ b/spec/spec_helper.cr @@ -0,0 +1,2 @@ +require "spec" +require "../src/golden-color" diff --git a/src/golden-color.cr b/src/golden-color.cr new file mode 100644 index 0000000..4e5c615 --- /dev/null +++ b/src/golden-color.cr @@ -0,0 +1,58 @@ +module GoldenColor + VERSION = "0.1.0" + + struct Color + getter r : Float64 + getter g : Float64 + getter b : Float64 + + def initialize(@r = 0, @g = 0, @b = 0) + end + + def self.from_rgb(r, g, b) + self.new r, g, b + end + + def self.from_hsv(h, s, v) + h *= 6.0 + chroma = s * v + x = chroma * (1.0 - (h.modulo(2.0) - 1.0).abs) + + r, g, b = if h >= 0.0 && h <= 1.0 + {chroma, x, 0.0} + elsif h >= 1.0 && h <= 2.0 + {x, chroma, 0.0} + elsif h >= 2.0 && h <= 3.0 + {0.0, chroma, x} + elsif h >= 3.0 && h <= 4.0 + {0.0, x, chroma} + elsif h >= 4.0 && h <= 5.0 + {x, 0.0, chroma} + else + {chroma, 0.0, x} + end + m = v - chroma + + self.new(r + m, g + m, b + m) + end + + def rgb + { r, g, b } + end + end + + class ColorGen + getter seed : Float64 + + def initialize(@saturation = 0.5, + @value = 0.95, + @seed = Random.rand(1.0)) + end + + def generate + color = Color.from_hsv @seed, @saturation, @value + @seed = (@seed + 0.618033988749895).modulo(1.0) + color + end + end +end