<commit_before>/**
 * @module Color
 * @submodule Creating & Reading
 * @for p5
 * @requires core
 * @requires constants
 * @requires color_conversion
 */

'use strict';

var p5 = require('../core/core');
var constants = require('../core/constants');
var color_conversion = require('./color_conversion');

/**
 * Each color stores the color mode and level maxes that applied at the
 * time of its construction. These are used to interpret the input arguments
 * (at construction and later for that instance of color) and to format the
 * output e.g. when saturation() is requested.
 *
 * Internally we store an array representing the ideal RGBA values in floating
 * point form, normalized from 0 to 1. From this we calculate the closest
 * screen color (RGBA levels from 0 to 255) and expose this to the renderer.
 *
 * We also cache normalized, floating point components of the color in various
 * representations as they are calculated. This is done to prevent repeating a
 * conversion that has already been performed.
 *
 * @class p5.Color
 * @constructor
 */
p5.Color = function(renderer, vals) {
  // Record color mode and maxes at time of construction.
  this._storeModeAndMaxes(renderer._colorMode, renderer._colorMaxes);

  // Calculate normalized RGBA values.
  if (
    this.mode !== constants.RGB &&
    this.mode !== constants.HSL &&
    this.mode !== constants.HSB
  ) {
    throw new Error(this.mode + ' is an invalid colorMode.');
  } else {
    this._array = p5.Color._parseInputs.apply(this, vals);
  }

  // Expose closest screen color.
  this._calculateLevels();
  this.name = 'p5.Color'; // for friendly debugger system
  return this;
};

/**
 * This function returns the color formatted as a string. This can be useful
 * for debugging, or for using p5.js with other libraries.
 * @method toString
 * @param {Number|String} [format] How the color string will be formatted.
 * Leaving this empty formats the string as rgba(r, g, b, a).
 * '#rgb' '#rgba' '#rrggbb' and '#rrggbbaa' format as hexadecimal color codes.
 * 'hex3' 'hex4' 'hex6' and 'hex8' return the same, without the '#''
 * 16 'hex' and 'web-color' are aliases of 'hex6'.
 * 'rgb' 'hsb' and 'hsl' return the color formatted as each color mode.
 * 'rgba' 'hsba' and 'hsla' are the same as above but with alpha channels.
 * 'rgb%' 'hsb%' 'hsl%' 'rgba%' 'hsba%' and 'hsla%' format as percentages.
 * @return {String} the formatted string
 * @example
 * <div>
 * <code>
 * var myColor;
 * function setup() {
 *   createCanvas(200, 200);
 *   stroke(255);
 *   myColor = color(100, 100, 250);
 *   fill(myColor);
 * }
 *
 * function draw() {
 *   text(myColor.toString(), 10, 10);
 *   text(myColor.toString('hex'), 10, 95);
 *   text(myColor.toString('rgba%'), 10, 180);
 * }
 * </code>
 * </div>
 *
 * @alt
 * canvas with text representation of color
 */
p5.Color.prototype.toString = function(format) {
  if (!this.hsba) this.hsba = color_conversion._rgbaToHSBA(this._array);
  if (!this.hsla) this.hsla = color_conversion._rgbaToHSLA(this._array);

  var a = this.levels;
  var f = this._array;
  var alpha = f[3]; // String representation uses normalized alpha
  var str = '';

  switch (format) {
    case '#rrggbb':
    case 'web-color':
      str = '#'; //fallthrough
    case 16:
    case 'hex':
    case 'hex6':
      return str.concat(
        a[0].toString(16),
        a[1].toString(16),
        a[2].toString(16)
      );

    case '#rrggbbaa':
      str = '#'; //fallthrough
    case 'hex8':
      return str.concat(
        a[0].toString(16),
        a[1].toString(16),
        a[2].toString(16),
        a[3].toString(16)
      );

    case '#rgb':
      str = '#'; //fallthrough
    case 'hex3':
      return str.concat(
        Math.floor(f[0] * 15).toString(16),
        Math.floor(f[1] * 15).toString(16),
        Math.floor(f[2] * 15).toString(16)
      );

    case '#rgba':
      str = '#'; //fallthrough
    case 'hex4':
      return str.concat(
        Math.floor(f[0] * 15).toString(16),
        Math.floor(f[1] * 15).toString(16),
        Math.floor(f[2] * 15).toString(16),
        Math.floor(f[3] * 15).toString(16)
      );

    case 'rgb':
      return 'rgb('.concat(a[0], ', ', a[1], ', ', a[2], ')');

    case 'rgb%':
      return 'rgb('.concat(
        (100 * f[0]).toPrecision(3),
        '%, ',
        (100 * f[1]).toPrecision(3),
        '%, ',<commit_msg>Prefix hex digit with 0 if value is less than 16.<commit_after>/**
 * @module Color
 * @submodule Creating & Reading
 * @for p5
 * @requires core
 * @requires constants
 * @requires color_conversion
 */

'use strict';

var p5 = require('../core/core');
var constants = require('../core/constants');
var color_conversion = require('./color_conversion');

/**
 * Each color stores the color mode and level maxes that applied at the
 * time of its construction. These are used to interpret the input arguments
 * (at construction and later for that instance of color) and to format the
 * output e.g. when saturation() is requested.
 *
 * Internally we store an array representing the ideal RGBA values in floating
 * point form, normalized from 0 to 1. From this we calculate the closest
 * screen color (RGBA levels from 0 to 255) and expose this to the renderer.
 *
 * We also cache normalized, floating point components of the color in various
 * representations as they are calculated. This is done to prevent repeating a
 * conversion that has already been performed.
 *
 * @class p5.Color
 * @constructor
 */
p5.Color = function(renderer, vals) {
  // Record color mode and maxes at time of construction.
  this._storeModeAndMaxes(renderer._colorMode, renderer._colorMaxes);

  // Calculate normalized RGBA values.
  if (
    this.mode !== constants.RGB &&
    this.mode !== constants.HSL &&
    this.mode !== constants.HSB
  ) {
    throw new Error(this.mode + ' is an invalid colorMode.');
  } else {
    this._array = p5.Color._parseInputs.apply(this, vals);
  }

  // Expose closest screen color.
  this._calculateLevels();
  this.name = 'p5.Color'; // for friendly debugger system
  return this;
};

/**
 * This function returns the color formatted as a string. This can be useful
 * for debugging, or for using p5.js with other libraries.
 * @method toString
 * @param {Number|String} [format] How the color string will be formatted.
 * Leaving this empty formats the string as rgba(r, g, b, a).
 * '#rgb' '#rgba' '#rrggbb' and '#rrggbbaa' format as hexadecimal color codes.
 * 'hex3' 'hex4' 'hex6' and 'hex8' return the same, without the '#''
 * 16 'hex' and 'web-color' are aliases of 'hex6'.
 * 'rgb' 'hsb' and 'hsl' return the color formatted as each color mode.
 * 'rgba' 'hsba' and 'hsla' are the same as above but with alpha channels.
 * 'rgb%' 'hsb%' 'hsl%' 'rgba%' 'hsba%' and 'hsla%' format as percentages.
 * @return {String} the formatted string
 * @example
 * <div>
 * <code>
 * var myColor;
 * function setup() {
 *   createCanvas(200, 200);
 *   stroke(255);
 *   myColor = color(100, 100, 250);
 *   fill(myColor);
 * }
 *
 * function draw() {
 *   text(myColor.toString(), 10, 10);
 *   text(myColor.toString('hex'), 10, 95);
 *   text(myColor.toString('rgba%'), 10, 180);
 * }
 * </code>
 * </div>
 *
 * @alt
 * canvas with text representation of color
 */
p5.Color.prototype.toString = function(format) {
  if (!this.hsba) this.hsba = color_conversion._rgbaToHSBA(this._array);
  if (!this.hsla) this.hsla = color_conversion._rgbaToHSLA(this._array);

  var a = this.levels;
  var f = this._array;
  var alpha = f[3]; // String representation uses normalized alpha
  var str = '';

  switch (format) {
    case '#rrggbb':
    case 'web-color':
      str = '#'; //fallthrough
    case 16:
    case '16':
    case 'hex':
    case 'hex6':
      return str.concat(
        a[0] < 16 ? '0'.concat(a[0].toString(16)) : a[0].toString(16),
        a[1] < 16 ? '0'.concat(a[1].toString(16)) : a[1].toString(16),
        a[2] < 16 ? '0'.concat(a[2].toString(16)) : a[2].toString(16)
      );

    case '#rrggbbaa':
      str = '#'; //fallthrough
    case 'hex8':
      return str.concat(
        a[0] < 16 ? '0'.concat(a[0].toString(16)) : a[0].toString(16),
        a[1] < 16 ? '0'.concat(a[1].toString(16)) : a[1].toString(16),
        a[2] < 16 ? '0'.concat(a[2].toString(16)) : a[2].toString(16),
        a[3] < 16 ? '0'.concat(a[2].toString(16)) : a[3].toString(16)
      );

    case '#rgb':
      str = '#'; //fallthrough
    case 'hex3':
      return str.concat(
        Math.floor(f[0] * 15).toString(16),
        Math.floor(f[1] * 15).toString(16),
        Math.floor(f[2] * 15).toString(16)
      );

    case '#rgba':
      str = '#'; //fallthrough
    case 'hex4':
      return str.concat(
        Math.floor(f[0] * 15).toString(16),
        Math.floor(f[1] * 15).toString(16),
        Math.floor(f[2] * 15).toString(16),
        Math.floor(f[3] * 15).toString(16)
      );

    case 'rgb':
      return 'rgb('.concat(a[0], ', ', a[1], ', ', a[2], ')');

    case 'rgb%':
      return 'rgb('.concat(
        (100 * f[0]).toPrecision(3),
        '%, ',
        (100 * f[1]).toPrecision(3),
        '%, ',
{'fraction_non_alphanumeric': 0.15794095049939633, 'fraction_numerical': 0.03402480518055098, 'mean_word_length': 2.9582971329278887, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '77122', 'n_tokens_mistral': 3215, 'n_tokens_neox': 2989, 'n_words': 911}