Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for 3-symbol hex strings. #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions Colours.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,32 @@ @implementation NSColor (Colours)
#pragma mark - Color from Hex
+ (instancetype)colorFromHexString:(NSString *)hexString
{
unsigned rgbValue = 0;
unsigned long long rgbValue = 0;
int base = 0;
int mask = 0;

hexString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
NSScanner *scanner = [NSScanner scannerWithString:hexString];
[scanner scanHexInt:&rgbValue];

return [[self class] colorWithR:((rgbValue & 0xFF0000) >> 16) G:((rgbValue & 0xFF00) >> 8) B:(rgbValue & 0xFF) A:1.0];
[scanner scanHexLongLong:&rgbValue];

switch (hexString.length) {
case 3:
base = 8;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function hasAnyCalc() {
return hasCalc("-moz-") || hasCalc("-ms-") || hasCalc("-o-") || hasCalc("-webkit-") || hasCalc();
}

  function calcHeight() {
    return window.innerHeight - {{#newScript}}190{{/newScript}}{{^newScript}}303{{/newScript}};
  }

  editor.setTheme("ace/theme/dawn");
  editor.getSession().setMode("ace/mode/javascript");

  {{#readOnly}}editor.setReadOnly(true);{{/readOnly}}
  {{^readOnly}}
  // TODO: Submit using AJAX
  $('#submit_code').click(function () {
    $('#source').val(editor.getValue());
    $('#code_form').submit();
  });
  {{/readOnly}}

  // Older browser JavaScript work-around for #136
  if (!hasAnyCalc()) {
    $("#editor").height(calcHeight());
    $(document).on("resize", function () {
      $("#editor").height(calcHeight);
    });
  }
});

})();
</script>

kincaid/

request_id=1&ios_base_sdk=9.3&platform=iPhone&app_name=26.iphone.com.tigervpns.hideme.ios.HideMe&u_so=p&js=afma-sdk-i-v7.6.0&ios_app_volume=1&ios_output_volume=0.344048&eid=318475625%2C46621136&ms=JWrkcPUl_IpumByXcxOud6yFysZvvAIBNHUclooCFb5hqW2yBQz5sjBIau1rDDJOWEQ37NGzhXiHutUuPswb4oQ-OwqwF3G-0WQwYIh2204cAo-

User-agent: *
Disallow: /admin/
Disallow: /mod/
Disallow: /flag/
Disallow: /vote/
Disallow: /auth/
Disallow: /github/
Disallow: /css/
Disallow: /dist/
Disallow: /fonts/
Disallow: /js/
Disallow: /redist/

mask = 0xF;
break;
case 6:
base = 16;
mask = 0xFF;
break;
default:
return nil;
}

CGFloat r = (CGFloat)(mask & (rgbValue >> base)) / (CGFloat)mask;
CGFloat g = (CGFloat)(mask & (rgbValue >> (base / 2))) / (CGFloat)mask;
CGFloat b = (CGFloat)(mask & (rgbValue >> 0)) / (CGFloat)mask;

return [[self class] colorWithRed:r green:g blue:b alpha:1.0];
}


Expand Down
20 changes: 17 additions & 3 deletions Colours.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,26 @@ public extension Color {
// MARK: - Color from Hex/RGBA/HSBA/CIE_LAB/CMYK
convenience init(hex: String) {
var rgbInt: UInt64 = 0
var base: UInt64 = 0
var mask: UInt64 = 0

let newHex = hex.stringByReplacingOccurrencesOfString("#", withString: "")
let scanner = NSScanner(string: newHex)
scanner.scanHexLongLong(&rgbInt)
let r: CGFloat = CGFloat((rgbInt & 0xFF0000) >> 16)/255.0
let g: CGFloat = CGFloat((rgbInt & 0x00FF00) >> 8)/255.0
let b: CGFloat = CGFloat(rgbInt & 0x0000FF)/255.0

switch (newHex.characters.count) {
case 3:
base = 8
mask = 0xF
default:
base = 16
mask = 0xFF
}

let r = CGFloat(mask & (rgbInt >> base)) / CGFloat(mask);
let g = CGFloat(mask & (rgbInt >> (base / 2))) / CGFloat(mask);
let b = CGFloat(mask & (rgbInt >> 0)) / CGFloat(mask);

self.init(red: r, green: g, blue: b, alpha: 1.0)
}

Expand Down