diff --git a/docs/colors.rst b/docs/colors.rst index ab02a0b..e0deab7 100644 --- a/docs/colors.rst +++ b/docs/colors.rst @@ -88,3 +88,25 @@ Sample: gradients .. raw:: html + +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: diff --git a/samples/colors/_switch_color.rb b/samples/colors/_switch_color.rb new file mode 100644 index 0000000..dab17a1 --- /dev/null +++ b/samples/colors/_switch_color.rb @@ -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 diff --git a/samples/colors/_switch_color_data.csv b/samples/colors/_switch_color_data.csv new file mode 100644 index 0000000..98b09d3 --- /dev/null +++ b/samples/colors/_switch_color_data.csv @@ -0,0 +1,3 @@ +Type Text +Snake This is a snake. +Lion This is a lion.