Skip to content

Commit

Permalink
Add sample for color switch in colors docs (#274)
Browse files Browse the repository at this point in the history
One of the things I used for a current project was a switch to change the color schemes quickly. I added the snippet in [Specifying Colors & Gradients](https://squib.readthedocs.io/en/latest/colors.html) and in the wiki: https://github.com/andymeneely/squib/wiki/Switch-card-background-or-invert-theme.


* Add sample for color switch in colors docs

* Update docs/colors.rst

Co-Authored-By: Andy Meneely <andy.meneely@gmail.com>

* Update docs/colors.rst

Co-Authored-By: Andy Meneely <andy.meneely@gmail.com>

* Update docs/colors.rst

Co-Authored-By: Andy Meneely <andy.meneely@gmail.com>

* Update docs/colors.rst

Co-Authored-By: Andy Meneely <andy.meneely@gmail.com>

* Update docs/colors.rst

Co-Authored-By: Andy Meneely <andy.meneely@gmail.com>

* Update docs/colors.rst

Co-Authored-By: Andy Meneely <andy.meneely@gmail.com>

* Add color switch sample and update docs

* Add color switch sample and update docs
  • Loading branch information
Karneades authored and andymeneely committed May 29, 2019
1 parent 74dfd85 commit d96cf71
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/colors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,25 @@ Sample: gradients
.. raw:: html

<img src="colors/gradient_00_expected.png" width=600 class="figure">

Sample: Switch color based on variable
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Say you have different card types with different colors or themes but you would like to change the theme quickly without changing all parameters for each method inside ``Squib::Deck.new()``.

You could use something like the following snippet to have one place to define the color/theme and use that in all map functions. The example inverts the color from white to black for one card type. Use a switch-statement inside the map function to differentiate multiple types.

It's possible to use that for e.g. background color, text color or to choose the correct SVG for that color scheme (e.g. use the white or the black version of an image). The sample shows how to define the color at one place, e.g. choose between white and black, to quickly change the color scheme for the cards:

Here's the data or see the tab-separated file in the sample directory:

===== ======================
Type Text
===== ======================
Snake This is a snake.
Lion This is a lion.
===== ======================

.. literalinclude:: ../samples/colors/_switch_color.rb
:language: ruby
:linenos:
33 changes: 33 additions & 0 deletions samples/colors/_switch_color.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'squib'

# Choose between black and white color theme for type snake
# * Allow using white snake cards with black text or
# black snake cards with white text
color = 'white'

cards = Squib.csv file: '_switch_color_data.csv', col_sep: "\t"

Squib::Deck.new cards: cards['Type'].size do

background_color = cards['Type'].map do |t|
if color == 'black' && t == "Snake" then
"black"
else
"white"
end
end
background color: background_color

text_color = cards['Type'].map do |t|
if color == 'black' && t == "Snake" then
"white"
else
"black"
end
end

text str: cards['Text'], color: text_color

save_png prefix: '_switch_color_sample_'

end
3 changes: 3 additions & 0 deletions samples/colors/_switch_color_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Type Text
Snake This is a snake.
Lion This is a lion.

0 comments on commit d96cf71

Please sign in to comment.