# RGB颜色转HEX进制与单位换算

110 篇文章 1 订阅
105 篇文章 0 订阅

RGB颜色转HEX进制：

function toHex(N) {
if (N==null) return "00";
N=parseInt(N); if (N==0 || isNaN(N)) return "00";
N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
return "0123456789ABCDEF".charAt((N-N%16)/16)
+ "0123456789ABCDEF".charAt(N%16);
}

function getColorStringFromRGB(r, g, b) {
return "#" + toHex(r) + toHex(g) + toHex(b);
}

c#：

public static string RgbToHex(int r, int g, int b)

{

}

priavate string ToHex(int? N)

{
if (N==null || N==0) return "00";
N=Math.Max(0,N);

N=Math.Min(N,255);

N=Math.Round(N);
return "0123456789ABCDEF".charAt((N-N%16)/16)
+ "0123456789ABCDEF".charAt(N%16);
}

public static string ColorStringFromRGB(int r, int g, int b)

{

return "#" + RgbToHex(r, g, b);

}

//下面的备用

function JSONBlocksToJSONArray(jsonBlocks) {
var jsonArray = '{';
var firstBlock = true;

for (var key in jsonBlocks) {
if (jsonBlocks.hasOwnProperty(key)) {
jsonArray += (firstBlock ? '' : ',') + '"' + key + '" : ' + jsonBlocks[key];
firstBlock = false;
}
}

jsonArray += '}';

return jsonArray;
}

//AI or PS JSON Color To RGB

convertColorToRGB : function (colorJSON) {
var pickerDesc = new ActionDescriptor();
pickerDesc.putBoolean( stringIDToTypeID( 'newDocPresetJSON'), true);
pickerDesc.putString( stringIDToTypeID( 'color' ), colorJSON);
pickerDesc.putEnumerated(stringIDToTypeID( 'colorSpace'), stringIDToTypeID( 'colorSpace'), stringIDToTypeID('RGB'));

var resultDesc = executeAction( stringIDToTypeID('convertColorToSpace'), pickerDesc, DialogModes.ALL );

return resultDesc.getString(stringIDToTypeID('color'));
}

function getRGBFromColorString(colorString) {
var r = 0,
g = 0,
b = 0;
try {
colorString = colorString.substr(colorString.indexOf("#") + 1); // Strip off '#'
// Ignore alpha
if (colorString.length > 6) {
colorString = colorString.substr(0, 6);
}
var val = parseInt(colorString, 16);
r = (val >> 16) & 255;
g = (val >> 8) & 255;
b = val & 255;
} catch (ignore) {}
return newRGB(r, g, b);
}

picasToPixels: function (picas) {
return Math.round(picas * 16);
}

cmToPixels: function (cms) {
return Math.round(cms * 37.79527559055);
}

inToPixels: function (inches) {
return Math.round(inches * 96.0000000000011);
}

mmToPixels: function (mm, resolution) {
var pt = mm * 2.83464566929134;
return pointsToPixels(pt, resolution);
}

pointsToPixels: function (pt, resolution) {
return Math.round((pt / 72) * resolution);
}
pixelsToPoints: function (px, resolution) {
return (px * 72) / resolution;
}

//换算为Points:

function ConvertUnitsToPoints(unitType, unitValue) {
if (unitType === MeasurementUnits.POINTS) {
return unitValue;
}

var pointsValue = unitValue;
switch (unitType) {
case MeasurementUnits.Q:
pointsValue = pointsValue * 0.7086614173228346;
break;
case MeasurementUnits.AMERICAN_POINTS:
pointsValue = pointsValue * (0.3514 / 25.4 * 72);
break;
case MeasurementUnits.MILLIMETERS:
pointsValue = (pointsValue * 72.0) / 25.4;
break;
case MeasurementUnits.HA:
pointsValue = pointsValue * 0.7086614173228346;
break;
case MeasurementUnits.BAI:
pointsValue = pointsValue * 6.336;
break;
case MeasurementUnits.U:
pointsValue = (pointsValue * 72.0 * 11.0) / 1000.0;
break;
case MeasurementUnits.MILS:
pointsValue = pointsValue * 0.072;
break;
case MeasurementUnits.PICAS:
pointsValue = pointsValue * 12.0;
break;
case MeasurementUnits.INCHES:
case MeasurementUnits.INCHES_DECIMAL:
pointsValue = pointsValue * 72.0;
break;
case MeasurementUnits.CENTIMETERS:
pointsValue = (pointsValue * 72.0) / 2.54;
break;
case MeasurementUnits.CICEROS:
pointsValue = pointsValue * 12.7878751998;
break;
case MeasurementUnits.AGATES:
pointsValue = (pointsValue * 72.0) / 14.0;
break;
case MeasurementUnits.PIXELS:
//No conversion required
break;
default:
break;
}
return pointsValue;
};

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

07-07 2140
04-15 9155

06-21 2475
09-07 179
10-30 5158
06-30 585
03-30 1573
11-20 6810
08-19 2356
07-13 2374
11-30 3549
08-07 733
11-30 1万+