Create initial version of shard.

This commit is contained in:
Danila Fedorin 2018-09-18 23:06:12 -07:00
commit 3d9cf7858b
9 changed files with 176 additions and 0 deletions

9
.editorconfig Normal file
View File

@ -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

9
.gitignore vendored Normal file
View File

@ -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

1
.travis.yml Normal file
View File

@ -0,0 +1 @@
language: crystal

21
LICENSE Normal file
View File

@ -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.

40
README.md Normal file
View File

@ -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

9
shard.yml Normal file
View File

@ -0,0 +1,9 @@
name: golden-color
version: 0.1.0
authors:
- Danila Fedorin <danila.fedorin@gmail.com>
crystal: 0.26.1
license: MIT

27
spec/golden-color_spec.cr Normal file
View File

@ -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

2
spec/spec_helper.cr Normal file
View File

@ -0,0 +1,2 @@
require "spec"
require "../src/golden-color"

58
src/golden-color.cr Normal file
View File

@ -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