File

projects/wms-framework/src/lib/media/color.ts

Description

SmColor class

Index

Properties
Methods

Properties

A
Type : number
Default value : 0
B
Type : number
Default value : 0
G
Type : number
Default value : 0
R
Type : number
Default value : 0

Methods

Private convertToString
convertToString()
Returns : string
Public toString
toString()

Returns the string representation of the SmColor.

Returns : string

{string}

export class SmColor {
  A = 0;
  R = 0;
  G = 0;
  B = 0;

  /**
   * Returns the string representation of the SmColor.
   *
   * @return {*}  {string}
   * @memberof SmColor
   * @wMethod ToString
   */
  public toString(): string {
    return this.convertToString();
  }

  private convertToString(): string {
    let redText = this.R.toString(16);
    let greenText = this.G.toString(16);
    let blueText = this.B.toString(16);
    let alfaText = this.A.toString(16) || 'FF';
    return formatColors(alfaText, redText, greenText, blueText);
  }
}

export function createColorFromArgb(
  alpha: number,
  red: number,
  green: number,
  blue: number
): SmColor {
  return Object.assign(new SmColor(), {
    A: alpha,
    R: red,
    G: green,
    B: blue,
  });
}

export function smColorToCssColor(color: SmColor): string {
  if (!color) return '';
  let redText = color.R.toString(16);
  let greenText = color.G.toString(16);
  let blueText = color.B.toString(16);
  let alfaText = color.A.toString(16) || 'FF';
  return formatColors(redText, greenText, blueText, alfaText);
}

function formatColors(
  color1: string,
  color2: string,
  color3: string,
  color4: string
): string {
  if (color1.length !== 2) {
    color1 = '0' + color1[color1.length - 1];
  }
  if (color2.length !== 2) {
    color2 = '0' + color2[color2.length - 1];
  }
  if (color3.length !== 2) {
    color3 = '0' + color3[color3.length - 1];
  }
  if (color4.length !== 2) {
    color4 = '0' + color4[color4.length - 1];
  }
  return ('#' + color1 + color2 + color3 + color4).toUpperCase();
}

export function smColorFromString(color: string): SmColor {
  if (!color || color === '') {
    return createColorFromArgb(255, 0.0, 0.0, 0.0);
  }
  let str1 = color;
  let result1 = 0;
  let result2 = 0;
  let result3 = 0;
  let result4 = 0;
  let str3 = str1.substring(0, 2) != '0x' ? str1 : str1.substring(2);
  let str2 = str1.substring(0, 1) != '#' ? str3 : str1.substring(1);
  for (let length = str2.length; length < 6; ++length) str2 += '0';
  if (str2.length == 6) {
    result1 = parseInt(str2.substring(0, 2), 16);
    result2 = parseInt(str2.substring(2, 4), 16);
    result3 = parseInt(str2.substring(4, 6), 16);
    return createColorFromArgb(255, result1, result2, result3);
  }
  if (str2.length != 8) {
    return createColorFromArgb(result4, result1, result2, result3);
  }
  result4 = parseInt(str2.substring(0, 2), 16);
  result1 = parseInt(str2.substring(2, 4), 16);
  result2 = parseInt(str2.substring(4, 6), 16);
  result3 = parseInt(str2.substring(6, 8), 16);
  return createColorFromArgb(result4, result1, result2, result3);
}

export const SmColors = {
  Red: createColorFromArgb(255, 255, 0, 0),
  Green: createColorFromArgb(255, 0, 128, 0),
  Blue: createColorFromArgb(255, 0, 0, 255),
  Black: createColorFromArgb(255, 0, 0, 0),
  White: createColorFromArgb(255, 255, 255, 255),
  LightGray: createColorFromArgb(255, 0xd3, 0xd3, 0xd3),
  LightYellow: createColorFromArgb(255, 255, 255, 224),
  Brown: createColorFromArgb(255, 165, 42, 42),
  Cyan: createColorFromArgb(255, 0, 255, 255),
  DarkGray: createColorFromArgb(255, 169, 169, 169),
  Gray: createColorFromArgb(255, 128, 128, 128),
  Magenta: createColorFromArgb(255, 255, 0, 255),
  Orange: createColorFromArgb(255, 255, 165, 0),
  Purple: createColorFromArgb(255, 128, 0, 128),
  Yellow: createColorFromArgb(255, 255, 255, 0),
  Transparent: createColorFromArgb(0, 0, 0, 0),
};

result-matching ""

    No results matching ""