Convert numeric brightness percentage into hex for insteon
428130
def brightness_to_hex(self, level):
        """"""
        level_int = int(level)
        new_int = int((level_int * 255)/100)
        new_level = format(new_int, '02X')
        self.logger.debug("brightness_to_hex: %s to %s", level, str(new_level))
        return str(new_level)
1326083
function setBrightnessOfLed(callback) {
    var ledIndex = 8;
    
    var setBrightnessOfLedInterval = setInterval(function() {
        if (ledIndex >= 0) {
            dot3k.barGraph.setBrightnessOfLed(ledIndex, 0);
            
            ledIndex--;
        } else {
            clearInterval(setBrightnessOfLedInterval);
            
            if (callback) {
                callback();
            }
        }
    }, 500);
}
613716
def _parse_led(self, keypad, component_xml):
    """"""
    component_num = int(component_xml.get('ComponentNumber'))
    led_num = component_num - 80
    led = Led(self._lutron, keypad,
              name=('LED %d' % led_num),
              led_num=led_num,
              component_num=component_num)
    return led
1086202
def rgb2hex(rgb):
    
    if len(rgb) > 3:
        rgb = rgb[:-1]
    return "#{0:02x}{1:02x}{2:02x}".format(*(int(v*255) for v in rgb))
1367513
public static function adjust_brightness( $hex, $steps ) {
		$hex = self::sanitize_hex( $hex, false );
		$steps = max( -255, min( 255, $steps ) );

		// Adjust number of steps and keep it inside 0 to 255.
		$red   = max( 0, min( 255, hexdec( substr( $hex, 0, 2 ) ) + $steps ) );
		$green = max( 0, min( 255, hexdec( substr( $hex, 2, 2 ) ) + $steps ) );
		$blue  = max( 0, min( 255, hexdec( substr( $hex, 4, 2 ) ) + $steps ) );

		$red_hex   = str_pad( dechex( $red ), 2, '0', STR_PAD_LEFT );
		$green_hex = str_pad( dechex( $green ), 2, '0', STR_PAD_LEFT );
		$blue_hex  = str_pad( dechex( $blue ), 2, '0', STR_PAD_LEFT );
		return self::sanitize_hex( $red_hex . $green_hex . $blue_hex );
	}
1334876
def to_percentage(number, rounding=2):
    
    number = float(number) * 100
    number_as_int = int(number)
    rounded = round(number, rounding)

    return '{}%'.format(number_as_int if number_as_int == rounded else rounded)
634394
def rgb_to_hex(Class, r, g, b):
        """"""
        return ("%2s%2s%2s" % 
                (hex(int(r)).lstrip('0x'), hex(int(g)).lstrip('0x'), hex(int(b)).lstrip('0x'))
            ).replace(' ', '0').upper()
976779
function changeDecColorToHex(color) {
  let hexColor = parseInt(color, 10);
  hexColor = hexColor.toString(16);
  hexColor = doubleZeroPad(hexColor);

  return hexColor;
}
396905
private int getColorDigit( int idx, CssFormatter formatter ) {
        Expression expression = get( idx );
        double d = expression.doubleValue( formatter );
        if( expression.getDataType( formatter ) == PERCENT ) {
            d *= 2.55;
        }
        return colorDigit(d);
    }
1045600
function convertHexColorToRgba(hex) {
  // Expand shorthand form (e.g. "#03F") to full form (e.g. "#0033FF")
  const shorthandRegex = /^#([a-f\d])([a-f\d])([a-f\d])$/i;
  const hexFull = `${hex}`.replace(shorthandRegex, (m, r, g, b) => {
    return r + r + g + g + b + b;
  });

  const result = /^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hexFull);
  if (!result) {
    // Return untouched if not a hex code
    return hex;
  }

  const r = parseInt(result[1], 16);
  const g = parseInt(result[2], 16);
  const b = parseInt(result[3], 16);
  const a = 1;

  return `rgba(${r}, ${g}, ${b}, ${a})`;
}
482840
def set_brightness(host, did, value, token=None):
    """"""
    urllib3.disable_warnings()
    if token:
        scheme = "https"
    if not token:
        scheme = "http"
        token = "1234567890"
    url = (
            scheme + '://' + host + '/gwr/gop.php?cmd=DeviceSendCommand&data=<gip><version>1</version><token>' + token + '</token><did>' + did + '</did><value>' + str(
        value) + '</value><type>level</type></gip>&fmt=xml')
    response = requests.get(url, verify=False)
    if response.status_code == '200':
        return True
    else:
        return False
512891
def hex_color(value):
    '''
    '''
    r = ((value >> (8 * 2)) & 255) / 255.0
    g = ((value >> (8 * 1)) & 255) / 255.0
    b = ((value >> (8 * 0)) & 255) / 255.0
    return (r, g, b)
83540
def _hex_to_octets(addr):
    '''
    
    '''
    return '{0}:{1}:{2}:{3}'.format(
        int(addr[6:8], 16),
        int(addr[4:6], 16),
        int(addr[2:4], 16),
        int(addr[0:2], 16),
    )
1154037
function adjustBrightness($hex, $steps)
    {
        // Steps should be between -255 and 255. Negative = darker, positive = lighter
        $steps = max(-255, min(255, $steps));

        // Normalize into a six character long hex string
        $hex = str_replace('#', '', $hex);
        if (strlen($hex) == 3) {
            $hex = str_repeat(substr($hex, 0, 1), 2) . str_repeat(substr($hex, 1, 1), 2) . str_repeat(substr($hex, 2, 1), 2);
        }

        // Split into three parts: R, G and B
        $color_parts = str_split($hex, 2);
        $return = '#';

        foreach ($color_parts as $color) {
            $color = hexdec($color); // Convert to decimal
            $color = max(0, min(255, $color + $steps)); // Adjust color
            $return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code
        }

        return $return;
    }
175247
function valueToX(value) {
    const
        rect = this.$.getBoundingClientRect(),
        percent = (value - this.min) / (this.max - this.min);
    return rect.width * percent;
}
82280
function stringify (values, type) {
	// Accept object from `color-parse`
	if (!Array.isArray(values) && values.values) {
		var color = values
		if (color.alpha === 1 || color.alpha === undefined) {
			return stringify(color.values, type || color.space)
		} else {
			var values = color.values.slice()
			values.push(color.alpha)
			return stringify(values, type || color.space)
		}
	}

	if (type === 'hex') {
		var res = values.slice(0,3).map(function (value) {
			return (value < 16 ? '0' : '') + value.toString(16);
		}).join('');

		if (res[0] === res[1] && res[2] === res[3] && res[4] === res[5]) res = res[0] + res[2] + res[4];

		return '#' + res;
	}

	if (type === 'keyword') {
		return nameValues[values];
	}

	if (type === 'adobe1') {
		return 'R:' + values[0] + ', G:' + values[1] + ', B:' + values[2];
	}

	if (type === 'adobe2') {
		return '(R' + values[0] + ' / G' + values[1] + ' / B' + values[2] + ')';
	}

	var isPercent, isAlphaSpace;

	//convert rgb to percents
	if (type === 'percent') {
		type = 'rgb';
		values = values.map(function (value, i) {
			if (i === 3) return value;
			return Math.round(value * 100 / 255);
		});

		isPercent = true;
		isAlphaSpace = true;
	}

	type = type || 'rgb';

	//catch hwb/hsl/hsv
	isPercent = isPercent ? isPercent : type[0] === 'h';

	//detect whether alpha-perfix is needed
	isAlphaSpace = isAlphaSpace || /rgb|hs[lv]/i.test(type);

	//normalize space name
	if (isAlphaSpace && type[type.length - 1] === 'a') {
		type = type.slice(0,-1);
	}

	//normalize alpha
	var alpha = 1;
	if (values.length > type.length) {
		alpha = values.pop();
	}

	//decide the space name
	var alphaPf = isAlphaSpace && alpha < 1 ? 'a' : '';

	//<hue> case requires percents
	var res = type + alphaPf;

	res += '(' + values.map(function (value, i) {
		if (isPercent && !(type[i] === 'h')) {
			return value + '%';
		}
		else {
			return value;
		}
	}).join(', ');

	res += ( alpha < 1 ? ', ' + alpha : '' ) + ')';

	return res;
}
92041
function hex(s) {
    var m = /rgba?\((\d+), (\d+), (\d+)/.exec(s),
      c = s.split("");
    if (m) {
      s = ( m[1] << 16 | m[2] << 8 | m[3] ).toString(16);
      while (s.length < 6)
        s = "0" + s;
    }
    return "#" + (s.length == 6 ? s : c[1] + c[1] + c[2] + c[2] + c[3] + c[3]);
  }