首页 > 其他分享 >RGB颜色转HEX进制与单位换算

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

时间:2023-02-27 21:08:36浏览次数:31  
标签:case function return 进制 HEX break RGB pointsValue MeasurementUnits


RGB颜色转HEX进制:

function rgbToHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
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)
{
return ToHex(r)+ToHex(g)+ToHex(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:
//alert('convertUnitsToPoints: Unknown unitType');
break;
}
return pointsValue;
};

标签:case,function,return,进制,HEX,break,RGB,pointsValue,MeasurementUnits
From: https://blog.51cto.com/JohnsonJu/6089156

相关文章