Skip to content

Commit

Permalink
feat: add Tint.RGB.complementary_color/1 (#2)
Browse files Browse the repository at this point in the history
* feat: add `Tint.RGB.complementary_color/1`

* docs: add complementary color to README and add doctest

* chore: update tint to 1.1 in readme
  • Loading branch information
tlux committed Sep 26, 2020
1 parent b4948a5 commit 87e5dfd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ An Elixir library allowing calculations with colors and conversions between
different colorspaces.

Currently supports the following color models:

* [RGB](https://en.wikipedia.org/wiki/RGB_color_space)
* [CMYK](https://en.wikipedia.org/wiki/CMYK_color_model)
* [HSV](https://en.wikipedia.org/wiki/HSL_and_HSV)
Expand All @@ -27,7 +28,7 @@ The package can be installed by adding `tint` to your list of dependencies in
```elixir
def deps do
[
{:tint, "~> 1.0"}
{:tint, "~> 1.1"}
]
end
```
Expand Down Expand Up @@ -228,6 +229,13 @@ iex> Tint.Lab.nearest_color(~K[#FF0000], [~K[#009900], ~K[#CC0000]])
#Tint.RGB<204,0,0 (#CC0000)>
```

### Complementary Color

```elixir
iex> Tint.RGB.complementary_color(~K[#FF0000])
#Tint.RGB<0,255,255 (#00FFFF)>
```

## Docs

The API docs can be found at [HexDocs](https://hexdocs.pm/tint).
20 changes: 20 additions & 0 deletions lib/tint/rgb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ defmodule Tint.RGB do
{color.red, color.green, color.blue}
end

# Complementary Color

@doc """
Calculates the complementary of the given RGB color.
## Example
iex> Tint.RGB.complementary_color(%Tint.RGB{red: 255, green: 0, blue: 0})
#Tint.RGB<0,255,255 (#00FFFF)>
"""
@doc since: "1.1.0"
@spec complementary_color(t) :: t
def complementary_color(%__MODULE__{} = color) do
channel_size = @channel_interval.max
red = channel_size - color.red
green = channel_size - color.green
blue = channel_size - color.blue
new(red, green, blue)
end

# Distance

@doc """
Expand Down
12 changes: 12 additions & 0 deletions test/tint/rgb_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,18 @@ defmodule Tint.RGBTest do
end
end

describe "complementary_color/1" do
test "get complementary color in RGB colorspace" do
assert RGB.complementary_color(~K[#FF0000]) == ~K[#00FFFF]
assert RGB.complementary_color(~K[#00FF00]) == ~K[#FF00FF]
assert RGB.complementary_color(~K[#0000FF]) == ~K[#FFFF00]
assert RGB.complementary_color(~K[#FF8800]) == ~K[#0077FF]
assert RGB.complementary_color(~K[#3378F9]) == ~K[#CC8706]
assert RGB.complementary_color(~K[#8800FF]) == ~K[#77FF00]
assert RGB.complementary_color(~K[#99AA77]) == ~K[#665588]
end
end

describe "to_hex/1" do
test "convert RGB color struct to hex code" do
assert RGB.to_hex(RGB.new(0, 0, 0)) == "#000000"
Expand Down

0 comments on commit 87e5dfd

Please sign in to comment.