Fledex.Color.Names.LoadUtils (fledex v0.7.0)

View Source

Some utility functions that can help when loading color names from a CSV file

Summary

Functions

Converts an asci string representing a byte into that byte

Converts an asci string with an integer into an integer.

Converts a hex string (potentially with a # sign) into an integer

This function is used by the Generator, but is not intended to be used otherwise it is for loading the CSV files in a decently generic way.

Converts a string (can be even unicode) into an asci atom

Functions

a2b(value)

@spec a2b(String.t()) :: 0..255

Converts an asci string representing a byte into that byte

The value is interpreted as a value between 0 and 255 except in the following special cases:

  • %: If the string contains a trailing percent sign, then
    the value is interpreted as a percent and stretched onto a byte
  • °: If the string contains a trailing degree sign, then
    the value is interpreted degree between 0 and 359 and mapped to a byte

a2i(value)

@spec a2i(String.t()) :: integer()

Converts an asci string with an integer into an integer.

Any non-digit charaters will be removed before the conversion

hexstr2i(hex_string)

@spec hexstr2i(String.t()) :: integer()

Converts a hex string (potentially with a # sign) into an integer

load_color_file(filename, converter, opts)

@spec load_color_file(
  String.t(),
  ([String.t() | integer()] -> Fledex.Color.Names.Types.color_struct_t()),
  keyword()
) :: %{required(atom()) => Fledex.Color.Names.Types.color_struct_t()}

This function is used by the Generator, but is not intended to be used otherwise it is for loading the CSV files in a decently generic way.

You have to specify the following parameters:

  • filename (mandatory): The color file you want to process
  • converter(mandatory): This is a function needs to do the conversion from the different parts of a line into a color_struct_t structure.

You can in addition specify the following options:

  • name_pattern (default: ~r/^.*$/i): which names you want to actually want to load. Any descriptive_name that does not match this pattern will be dropped. You can for example only load the rows starting with an a.
  • drop (default: 1): how many header rows should be dropped. This allows to remove the header in a CSV file.
  • splitter_opts(default: []): The ops that will be used when splitting a line into the different parts. On one side it should contain the :separator (defaults to , as used in a proper CSV) and any split_opts as used by String.split/3.
  • module(default: :unknown): The name of the module that created the color map. This will automatically be added to the color_struct_t structure.
  • comment_pattern (default: ~r/^s*#.*$/): A pattern that will filter out any lines defined as comment lines (similar to code where the line might start with a #).

str2atom(name)

@spec str2atom(String.t()) :: atom()

Converts a string (can be even unicode) into an asci atom

It performs the following steps:

  • normalizes the string (NFD form)
  • replaces any non-alpha-numeric characters with underscores (except at the start and end)
  • downcases it
  • converts it to an atom