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

阿山杂谈 同时被 2 个专栏收录
110 篇文章 1 订阅
105 篇文章 0 订阅

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;
};

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

©️2021 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值